blob: 1b4c22a9e0630b9aef5858b78dd11e0cd3fa622e [file] [log] [blame]
Christian Heimes44720832008-05-26 13:01:01 +00001/* PyBytes (bytearray) implementation */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
5#include "structmember.h"
6#include "bytes_methods.h"
7
8static PyByteArrayObject *nullbytes = NULL;
Antoine Pitroubb667d42010-01-17 12:31:10 +00009char _PyByteArray_empty_string[] = "";
Christian Heimes44720832008-05-26 13:01:01 +000010
11void
12PyByteArray_Fini(void)
13{
14 Py_CLEAR(nullbytes);
15}
16
17int
18PyByteArray_Init(void)
19{
20 nullbytes = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
21 if (nullbytes == NULL)
22 return 0;
23 nullbytes->ob_bytes = NULL;
24 Py_SIZE(nullbytes) = nullbytes->ob_alloc = 0;
25 nullbytes->ob_exports = 0;
26 return 1;
27}
28
29/* end nullbytes support */
30
31/* Helpers */
32
33static int
34_getbytevalue(PyObject* arg, int *value)
35{
36 long face_value;
37
Georg Brandl3e483f62008-07-16 22:57:41 +000038 if (PyBytes_CheckExact(arg)) {
Christian Heimes44720832008-05-26 13:01:01 +000039 if (Py_SIZE(arg) != 1) {
40 PyErr_SetString(PyExc_ValueError, "string must be of size 1");
41 return 0;
42 }
Georg Brandl3e483f62008-07-16 22:57:41 +000043 *value = Py_CHARMASK(((PyBytesObject*)arg)->ob_sval[0]);
44 return 1;
45 }
46 else if (PyInt_Check(arg) || PyLong_Check(arg)) {
47 face_value = PyLong_AsLong(arg);
Christian Heimes44720832008-05-26 13:01:01 +000048 }
49 else {
Georg Brandl3e483f62008-07-16 22:57:41 +000050 PyObject *index = PyNumber_Index(arg);
51 if (index == NULL) {
52 PyErr_Format(PyExc_TypeError,
53 "an integer or string of size 1 is required");
54 return 0;
55 }
56 face_value = PyLong_AsLong(index);
57 Py_DECREF(index);
58 }
Georg Brandl3e483f62008-07-16 22:57:41 +000059
60 if (face_value < 0 || face_value >= 256) {
Georg Brandl3238a3e2008-07-16 23:17:46 +000061 /* this includes the OverflowError in case the long is too large */
Georg Brandl3e483f62008-07-16 22:57:41 +000062 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Christian Heimes44720832008-05-26 13:01:01 +000063 return 0;
64 }
65
66 *value = face_value;
67 return 1;
68}
69
70static Py_ssize_t
71bytes_buffer_getreadbuf(PyByteArrayObject *self, Py_ssize_t index, const void **ptr)
72{
73 if ( index != 0 ) {
74 PyErr_SetString(PyExc_SystemError,
75 "accessing non-existent bytes segment");
76 return -1;
77 }
Antoine Pitroubb667d42010-01-17 12:31:10 +000078 *ptr = (void *)PyByteArray_AS_STRING(self);
Christian Heimes44720832008-05-26 13:01:01 +000079 return Py_SIZE(self);
80}
81
82static Py_ssize_t
83bytes_buffer_getwritebuf(PyByteArrayObject *self, Py_ssize_t index, const void **ptr)
84{
85 if ( index != 0 ) {
86 PyErr_SetString(PyExc_SystemError,
87 "accessing non-existent bytes segment");
88 return -1;
89 }
Antoine Pitroubb667d42010-01-17 12:31:10 +000090 *ptr = (void *)PyByteArray_AS_STRING(self);
Christian Heimes44720832008-05-26 13:01:01 +000091 return Py_SIZE(self);
92}
93
94static Py_ssize_t
95bytes_buffer_getsegcount(PyByteArrayObject *self, Py_ssize_t *lenp)
96{
97 if ( lenp )
98 *lenp = Py_SIZE(self);
99 return 1;
100}
101
102static Py_ssize_t
103bytes_buffer_getcharbuf(PyByteArrayObject *self, Py_ssize_t index, const char **ptr)
104{
105 if ( index != 0 ) {
106 PyErr_SetString(PyExc_SystemError,
107 "accessing non-existent bytes segment");
108 return -1;
109 }
Antoine Pitroubb667d42010-01-17 12:31:10 +0000110 *ptr = PyByteArray_AS_STRING(self);
Christian Heimes44720832008-05-26 13:01:01 +0000111 return Py_SIZE(self);
112}
113
114static int
115bytes_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
116{
117 int ret;
118 void *ptr;
119 if (view == NULL) {
120 obj->ob_exports++;
121 return 0;
122 }
Antoine Pitroubb667d42010-01-17 12:31:10 +0000123 ptr = (void *) PyByteArray_AS_STRING(obj);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000124 ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
Christian Heimes44720832008-05-26 13:01:01 +0000125 if (ret >= 0) {
126 obj->ob_exports++;
127 }
128 return ret;
129}
130
131static void
132bytes_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
133{
134 obj->ob_exports--;
135}
136
137static Py_ssize_t
138_getbuffer(PyObject *obj, Py_buffer *view)
139{
140 PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer;
141
142 if (buffer == NULL || buffer->bf_getbuffer == NULL)
143 {
144 PyErr_Format(PyExc_TypeError,
145 "Type %.100s doesn't support the buffer API",
146 Py_TYPE(obj)->tp_name);
147 return -1;
148 }
149
150 if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
151 return -1;
152 return view->len;
153}
154
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000155static int
156_canresize(PyByteArrayObject *self)
157{
158 if (self->ob_exports > 0) {
159 PyErr_SetString(PyExc_BufferError,
160 "Existing exports of data: object cannot be re-sized");
161 return 0;
162 }
163 return 1;
164}
165
Christian Heimes44720832008-05-26 13:01:01 +0000166/* Direct API functions */
167
168PyObject *
169PyByteArray_FromObject(PyObject *input)
170{
171 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
172 input, NULL);
173}
174
175PyObject *
176PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
177{
178 PyByteArrayObject *new;
179 Py_ssize_t alloc;
180
181 if (size < 0) {
182 PyErr_SetString(PyExc_SystemError,
183 "Negative size passed to PyByteArray_FromStringAndSize");
184 return NULL;
185 }
186
187 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
188 if (new == NULL)
189 return NULL;
190
191 if (size == 0) {
192 new->ob_bytes = NULL;
193 alloc = 0;
194 }
195 else {
196 alloc = size + 1;
197 new->ob_bytes = PyMem_Malloc(alloc);
198 if (new->ob_bytes == NULL) {
199 Py_DECREF(new);
200 return PyErr_NoMemory();
201 }
Antoine Pitroubb667d42010-01-17 12:31:10 +0000202 if (bytes != NULL && size > 0)
Christian Heimes44720832008-05-26 13:01:01 +0000203 memcpy(new->ob_bytes, bytes, size);
204 new->ob_bytes[size] = '\0'; /* Trailing null byte */
205 }
206 Py_SIZE(new) = size;
207 new->ob_alloc = alloc;
208 new->ob_exports = 0;
209
210 return (PyObject *)new;
211}
212
213Py_ssize_t
214PyByteArray_Size(PyObject *self)
215{
216 assert(self != NULL);
217 assert(PyByteArray_Check(self));
218
219 return PyByteArray_GET_SIZE(self);
220}
221
222char *
223PyByteArray_AsString(PyObject *self)
224{
225 assert(self != NULL);
226 assert(PyByteArray_Check(self));
227
228 return PyByteArray_AS_STRING(self);
229}
230
231int
232PyByteArray_Resize(PyObject *self, Py_ssize_t size)
233{
234 void *sval;
235 Py_ssize_t alloc = ((PyByteArrayObject *)self)->ob_alloc;
236
237 assert(self != NULL);
238 assert(PyByteArray_Check(self));
239 assert(size >= 0);
240
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000241 if (size == Py_SIZE(self)) {
242 return 0;
243 }
244 if (!_canresize((PyByteArrayObject *)self)) {
245 return -1;
246 }
247
Christian Heimes44720832008-05-26 13:01:01 +0000248 if (size < alloc / 2) {
249 /* Major downsize; resize down to exact size */
250 alloc = size + 1;
251 }
252 else if (size < alloc) {
253 /* Within allocated size; quick exit */
254 Py_SIZE(self) = size;
255 ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null */
256 return 0;
257 }
258 else if (size <= alloc * 1.125) {
259 /* Moderate upsize; overallocate similar to list_resize() */
260 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
261 }
262 else {
263 /* Major upsize; resize up to exact size */
264 alloc = size + 1;
265 }
266
Christian Heimes44720832008-05-26 13:01:01 +0000267 sval = PyMem_Realloc(((PyByteArrayObject *)self)->ob_bytes, alloc);
268 if (sval == NULL) {
269 PyErr_NoMemory();
270 return -1;
271 }
272
273 ((PyByteArrayObject *)self)->ob_bytes = sval;
274 Py_SIZE(self) = size;
275 ((PyByteArrayObject *)self)->ob_alloc = alloc;
276 ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */
277
278 return 0;
279}
280
281PyObject *
282PyByteArray_Concat(PyObject *a, PyObject *b)
283{
284 Py_ssize_t size;
285 Py_buffer va, vb;
286 PyByteArrayObject *result = NULL;
287
288 va.len = -1;
289 vb.len = -1;
290 if (_getbuffer(a, &va) < 0 ||
291 _getbuffer(b, &vb) < 0) {
292 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
293 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
294 goto done;
295 }
296
297 size = va.len + vb.len;
298 if (size < 0) {
299 return PyErr_NoMemory();
300 goto done;
301 }
302
303 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
304 if (result != NULL) {
305 memcpy(result->ob_bytes, va.buf, va.len);
306 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
307 }
308
309 done:
310 if (va.len != -1)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000311 PyBuffer_Release(&va);
Christian Heimes44720832008-05-26 13:01:01 +0000312 if (vb.len != -1)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000313 PyBuffer_Release(&vb);
Christian Heimes44720832008-05-26 13:01:01 +0000314 return (PyObject *)result;
315}
316
317/* Functions stuffed into the type object */
318
319static Py_ssize_t
320bytes_length(PyByteArrayObject *self)
321{
322 return Py_SIZE(self);
323}
324
325static PyObject *
326bytes_iconcat(PyByteArrayObject *self, PyObject *other)
327{
328 Py_ssize_t mysize;
329 Py_ssize_t size;
330 Py_buffer vo;
331
332 if (_getbuffer(other, &vo) < 0) {
333 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
334 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
335 return NULL;
336 }
337
338 mysize = Py_SIZE(self);
339 size = mysize + vo.len;
340 if (size < 0) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000341 PyBuffer_Release(&vo);
Christian Heimes44720832008-05-26 13:01:01 +0000342 return PyErr_NoMemory();
343 }
344 if (size < self->ob_alloc) {
345 Py_SIZE(self) = size;
346 self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */
347 }
348 else if (PyByteArray_Resize((PyObject *)self, size) < 0) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000349 PyBuffer_Release(&vo);
Christian Heimes44720832008-05-26 13:01:01 +0000350 return NULL;
351 }
352 memcpy(self->ob_bytes + mysize, vo.buf, vo.len);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000353 PyBuffer_Release(&vo);
Christian Heimes44720832008-05-26 13:01:01 +0000354 Py_INCREF(self);
355 return (PyObject *)self;
356}
357
358static PyObject *
359bytes_repeat(PyByteArrayObject *self, Py_ssize_t count)
360{
361 PyByteArrayObject *result;
362 Py_ssize_t mysize;
363 Py_ssize_t size;
364
365 if (count < 0)
366 count = 0;
367 mysize = Py_SIZE(self);
368 size = mysize * count;
369 if (count != 0 && size / count != mysize)
370 return PyErr_NoMemory();
371 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
372 if (result != NULL && size != 0) {
373 if (mysize == 1)
374 memset(result->ob_bytes, self->ob_bytes[0], size);
375 else {
376 Py_ssize_t i;
377 for (i = 0; i < count; i++)
378 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
379 }
380 }
381 return (PyObject *)result;
382}
383
384static PyObject *
385bytes_irepeat(PyByteArrayObject *self, Py_ssize_t count)
386{
387 Py_ssize_t mysize;
388 Py_ssize_t size;
389
390 if (count < 0)
391 count = 0;
392 mysize = Py_SIZE(self);
393 size = mysize * count;
394 if (count != 0 && size / count != mysize)
395 return PyErr_NoMemory();
396 if (size < self->ob_alloc) {
397 Py_SIZE(self) = size;
398 self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */
399 }
400 else if (PyByteArray_Resize((PyObject *)self, size) < 0)
401 return NULL;
402
403 if (mysize == 1)
404 memset(self->ob_bytes, self->ob_bytes[0], size);
405 else {
406 Py_ssize_t i;
407 for (i = 1; i < count; i++)
408 memcpy(self->ob_bytes + i*mysize, self->ob_bytes, mysize);
409 }
410
411 Py_INCREF(self);
412 return (PyObject *)self;
413}
414
415static PyObject *
416bytes_getitem(PyByteArrayObject *self, Py_ssize_t i)
417{
418 if (i < 0)
419 i += Py_SIZE(self);
420 if (i < 0 || i >= Py_SIZE(self)) {
421 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
422 return NULL;
423 }
424 return PyInt_FromLong((unsigned char)(self->ob_bytes[i]));
425}
426
427static PyObject *
Georg Brandl3e483f62008-07-16 22:57:41 +0000428bytes_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes44720832008-05-26 13:01:01 +0000429{
Georg Brandl3e483f62008-07-16 22:57:41 +0000430 if (PyIndex_Check(index)) {
431 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes44720832008-05-26 13:01:01 +0000432
433 if (i == -1 && PyErr_Occurred())
434 return NULL;
435
436 if (i < 0)
437 i += PyByteArray_GET_SIZE(self);
438
439 if (i < 0 || i >= Py_SIZE(self)) {
440 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
441 return NULL;
442 }
443 return PyInt_FromLong((unsigned char)(self->ob_bytes[i]));
444 }
Georg Brandl3e483f62008-07-16 22:57:41 +0000445 else if (PySlice_Check(index)) {
Christian Heimes44720832008-05-26 13:01:01 +0000446 Py_ssize_t start, stop, step, slicelength, cur, i;
Georg Brandl3e483f62008-07-16 22:57:41 +0000447 if (PySlice_GetIndicesEx((PySliceObject *)index,
Christian Heimes44720832008-05-26 13:01:01 +0000448 PyByteArray_GET_SIZE(self),
449 &start, &stop, &step, &slicelength) < 0) {
450 return NULL;
451 }
452
453 if (slicelength <= 0)
454 return PyByteArray_FromStringAndSize("", 0);
455 else if (step == 1) {
456 return PyByteArray_FromStringAndSize(self->ob_bytes + start,
457 slicelength);
458 }
459 else {
460 char *source_buf = PyByteArray_AS_STRING(self);
461 char *result_buf = (char *)PyMem_Malloc(slicelength);
462 PyObject *result;
463
464 if (result_buf == NULL)
465 return PyErr_NoMemory();
466
467 for (cur = start, i = 0; i < slicelength;
468 cur += step, i++) {
469 result_buf[i] = source_buf[cur];
470 }
471 result = PyByteArray_FromStringAndSize(result_buf, slicelength);
472 PyMem_Free(result_buf);
473 return result;
474 }
475 }
476 else {
477 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers");
478 return NULL;
479 }
480}
481
482static int
483bytes_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
484 PyObject *values)
485{
486 Py_ssize_t avail, needed;
487 void *bytes;
488 Py_buffer vbytes;
489 int res = 0;
490
491 vbytes.len = -1;
492 if (values == (PyObject *)self) {
493 /* Make a copy and call this function recursively */
494 int err;
495 values = PyByteArray_FromObject(values);
496 if (values == NULL)
497 return -1;
498 err = bytes_setslice(self, lo, hi, values);
499 Py_DECREF(values);
500 return err;
501 }
502 if (values == NULL) {
503 /* del b[lo:hi] */
504 bytes = NULL;
505 needed = 0;
506 }
507 else {
508 if (_getbuffer(values, &vbytes) < 0) {
509 PyErr_Format(PyExc_TypeError,
Neal Norwitzc86b54c2008-07-20 19:35:23 +0000510 "can't set bytearray slice from %.100s",
Christian Heimes44720832008-05-26 13:01:01 +0000511 Py_TYPE(values)->tp_name);
512 return -1;
513 }
514 needed = vbytes.len;
515 bytes = vbytes.buf;
516 }
517
518 if (lo < 0)
519 lo = 0;
520 if (hi < lo)
521 hi = lo;
522 if (hi > Py_SIZE(self))
523 hi = Py_SIZE(self);
524
525 avail = hi - lo;
526 if (avail < 0)
527 lo = hi = avail = 0;
528
529 if (avail != needed) {
530 if (avail > needed) {
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000531 if (!_canresize(self)) {
532 res = -1;
533 goto finish;
534 }
Christian Heimes44720832008-05-26 13:01:01 +0000535 /*
536 0 lo hi old_size
537 | |<----avail----->|<-----tomove------>|
538 | |<-needed->|<-----tomove------>|
539 0 lo new_hi new_size
540 */
541 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
542 Py_SIZE(self) - hi);
543 }
544 /* XXX(nnorwitz): need to verify this can't overflow! */
545 if (PyByteArray_Resize((PyObject *)self,
546 Py_SIZE(self) + needed - avail) < 0) {
547 res = -1;
548 goto finish;
549 }
550 if (avail < needed) {
551 /*
552 0 lo hi old_size
553 | |<-avail->|<-----tomove------>|
554 | |<----needed---->|<-----tomove------>|
555 0 lo new_hi new_size
556 */
557 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
558 Py_SIZE(self) - lo - needed);
559 }
560 }
561
562 if (needed > 0)
563 memcpy(self->ob_bytes + lo, bytes, needed);
564
565
566 finish:
567 if (vbytes.len != -1)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000568 PyBuffer_Release(&vbytes);
Christian Heimes44720832008-05-26 13:01:01 +0000569 return res;
570}
571
572static int
573bytes_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
574{
575 int ival;
576
577 if (i < 0)
578 i += Py_SIZE(self);
579
580 if (i < 0 || i >= Py_SIZE(self)) {
581 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
582 return -1;
583 }
584
585 if (value == NULL)
586 return bytes_setslice(self, i, i+1, NULL);
587
588 if (!_getbytevalue(value, &ival))
589 return -1;
590
591 self->ob_bytes[i] = ival;
592 return 0;
593}
594
595static int
Georg Brandl3e483f62008-07-16 22:57:41 +0000596bytes_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes44720832008-05-26 13:01:01 +0000597{
598 Py_ssize_t start, stop, step, slicelen, needed;
599 char *bytes;
600
Georg Brandl3e483f62008-07-16 22:57:41 +0000601 if (PyIndex_Check(index)) {
602 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes44720832008-05-26 13:01:01 +0000603
604 if (i == -1 && PyErr_Occurred())
605 return -1;
606
607 if (i < 0)
608 i += PyByteArray_GET_SIZE(self);
609
610 if (i < 0 || i >= Py_SIZE(self)) {
611 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
612 return -1;
613 }
614
615 if (values == NULL) {
616 /* Fall through to slice assignment */
617 start = i;
618 stop = i + 1;
619 step = 1;
620 slicelen = 1;
621 }
622 else {
Georg Brandl3e483f62008-07-16 22:57:41 +0000623 int ival;
624 if (!_getbytevalue(values, &ival))
Christian Heimes44720832008-05-26 13:01:01 +0000625 return -1;
Christian Heimes44720832008-05-26 13:01:01 +0000626 self->ob_bytes[i] = (char)ival;
627 return 0;
628 }
629 }
Georg Brandl3e483f62008-07-16 22:57:41 +0000630 else if (PySlice_Check(index)) {
631 if (PySlice_GetIndicesEx((PySliceObject *)index,
Christian Heimes44720832008-05-26 13:01:01 +0000632 PyByteArray_GET_SIZE(self),
633 &start, &stop, &step, &slicelen) < 0) {
634 return -1;
635 }
636 }
637 else {
638 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer");
639 return -1;
640 }
641
642 if (values == NULL) {
643 bytes = NULL;
644 needed = 0;
645 }
646 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
647 /* Make a copy an call this function recursively */
648 int err;
649 values = PyByteArray_FromObject(values);
650 if (values == NULL)
651 return -1;
Georg Brandl3e483f62008-07-16 22:57:41 +0000652 err = bytes_ass_subscript(self, index, values);
Christian Heimes44720832008-05-26 13:01:01 +0000653 Py_DECREF(values);
654 return err;
655 }
656 else {
657 assert(PyByteArray_Check(values));
658 bytes = ((PyByteArrayObject *)values)->ob_bytes;
659 needed = Py_SIZE(values);
660 }
661 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
662 if ((step < 0 && start < stop) ||
663 (step > 0 && start > stop))
664 stop = start;
665 if (step == 1) {
666 if (slicelen != needed) {
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000667 if (!_canresize(self))
668 return -1;
Christian Heimes44720832008-05-26 13:01:01 +0000669 if (slicelen > needed) {
670 /*
671 0 start stop old_size
672 | |<---slicelen--->|<-----tomove------>|
673 | |<-needed->|<-----tomove------>|
674 0 lo new_hi new_size
675 */
676 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,
677 Py_SIZE(self) - stop);
678 }
679 if (PyByteArray_Resize((PyObject *)self,
680 Py_SIZE(self) + needed - slicelen) < 0)
681 return -1;
682 if (slicelen < needed) {
683 /*
684 0 lo hi old_size
685 | |<-avail->|<-----tomove------>|
686 | |<----needed---->|<-----tomove------>|
687 0 lo new_hi new_size
688 */
689 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,
690 Py_SIZE(self) - start - needed);
691 }
692 }
693
694 if (needed > 0)
695 memcpy(self->ob_bytes + start, bytes, needed);
696
697 return 0;
698 }
699 else {
700 if (needed == 0) {
701 /* Delete slice */
702 Py_ssize_t cur, i;
703
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000704 if (!_canresize(self))
705 return -1;
Christian Heimes44720832008-05-26 13:01:01 +0000706 if (step < 0) {
707 stop = start + 1;
708 start = stop + step * (slicelen - 1) - 1;
709 step = -step;
710 }
711 for (cur = start, i = 0;
712 i < slicelen; cur += step, i++) {
713 Py_ssize_t lim = step - 1;
714
715 if (cur + step >= PyByteArray_GET_SIZE(self))
716 lim = PyByteArray_GET_SIZE(self) - cur - 1;
717
718 memmove(self->ob_bytes + cur - i,
719 self->ob_bytes + cur + 1, lim);
720 }
721 /* Move the tail of the bytes, in one chunk */
722 cur = start + slicelen*step;
723 if (cur < PyByteArray_GET_SIZE(self)) {
724 memmove(self->ob_bytes + cur - slicelen,
725 self->ob_bytes + cur,
726 PyByteArray_GET_SIZE(self) - cur);
727 }
728 if (PyByteArray_Resize((PyObject *)self,
729 PyByteArray_GET_SIZE(self) - slicelen) < 0)
730 return -1;
731
732 return 0;
733 }
734 else {
735 /* Assign slice */
736 Py_ssize_t cur, i;
737
738 if (needed != slicelen) {
739 PyErr_Format(PyExc_ValueError,
740 "attempt to assign bytes of size %zd "
741 "to extended slice of size %zd",
742 needed, slicelen);
743 return -1;
744 }
745 for (cur = start, i = 0; i < slicelen; cur += step, i++)
746 self->ob_bytes[cur] = bytes[i];
747 return 0;
748 }
749 }
750}
751
752static int
753bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
754{
755 static char *kwlist[] = {"source", "encoding", "errors", 0};
756 PyObject *arg = NULL;
757 const char *encoding = NULL;
758 const char *errors = NULL;
759 Py_ssize_t count;
760 PyObject *it;
761 PyObject *(*iternext)(PyObject *);
762
763 if (Py_SIZE(self) != 0) {
764 /* Empty previous contents (yes, do this first of all!) */
765 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
766 return -1;
767 }
768
769 /* Parse arguments */
Neal Norwitzc86b54c2008-07-20 19:35:23 +0000770 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes44720832008-05-26 13:01:01 +0000771 &arg, &encoding, &errors))
772 return -1;
773
774 /* Make a quick exit if no first argument */
775 if (arg == NULL) {
776 if (encoding != NULL || errors != NULL) {
777 PyErr_SetString(PyExc_TypeError,
778 "encoding or errors without sequence argument");
779 return -1;
780 }
781 return 0;
782 }
783
784 if (PyBytes_Check(arg)) {
785 PyObject *new, *encoded;
786 if (encoding != NULL) {
787 encoded = PyCodec_Encode(arg, encoding, errors);
788 if (encoded == NULL)
789 return -1;
790 assert(PyBytes_Check(encoded));
791 }
792 else {
793 encoded = arg;
794 Py_INCREF(arg);
795 }
796 new = bytes_iconcat(self, arg);
797 Py_DECREF(encoded);
798 if (new == NULL)
799 return -1;
800 Py_DECREF(new);
801 return 0;
802 }
803
804 if (PyUnicode_Check(arg)) {
805 /* Encode via the codec registry */
806 PyObject *encoded, *new;
807 if (encoding == NULL) {
808 PyErr_SetString(PyExc_TypeError,
809 "unicode argument without an encoding");
810 return -1;
811 }
812 encoded = PyCodec_Encode(arg, encoding, errors);
813 if (encoded == NULL)
814 return -1;
815 assert(PyBytes_Check(encoded));
816 new = bytes_iconcat(self, encoded);
817 Py_DECREF(encoded);
818 if (new == NULL)
819 return -1;
820 Py_DECREF(new);
821 return 0;
822 }
823
824 /* If it's not unicode, there can't be encoding or errors */
825 if (encoding != NULL || errors != NULL) {
826 PyErr_SetString(PyExc_TypeError,
827 "encoding or errors without a string argument");
828 return -1;
829 }
830
831 /* Is it an int? */
832 count = PyNumber_AsSsize_t(arg, PyExc_ValueError);
833 if (count == -1 && PyErr_Occurred())
834 PyErr_Clear();
835 else {
836 if (count < 0) {
837 PyErr_SetString(PyExc_ValueError, "negative count");
838 return -1;
839 }
840 if (count > 0) {
841 if (PyByteArray_Resize((PyObject *)self, count))
842 return -1;
843 memset(self->ob_bytes, 0, count);
844 }
845 return 0;
846 }
847
848 /* Use the buffer API */
849 if (PyObject_CheckBuffer(arg)) {
850 Py_ssize_t size;
851 Py_buffer view;
852 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
853 return -1;
854 size = view.len;
855 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
856 if (PyBuffer_ToContiguous(self->ob_bytes, &view, size, 'C') < 0)
857 goto fail;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000858 PyBuffer_Release(&view);
Christian Heimes44720832008-05-26 13:01:01 +0000859 return 0;
860 fail:
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000861 PyBuffer_Release(&view);
Christian Heimes44720832008-05-26 13:01:01 +0000862 return -1;
863 }
864
865 /* XXX Optimize this if the arguments is a list, tuple */
866
867 /* Get the iterator */
868 it = PyObject_GetIter(arg);
869 if (it == NULL)
870 return -1;
871 iternext = *Py_TYPE(it)->tp_iternext;
872
873 /* Run the iterator to exhaustion */
874 for (;;) {
875 PyObject *item;
Georg Brandl3e758462008-07-16 23:10:05 +0000876 int rc, value;
Christian Heimes44720832008-05-26 13:01:01 +0000877
878 /* Get the next item */
879 item = iternext(it);
880 if (item == NULL) {
881 if (PyErr_Occurred()) {
882 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
883 goto error;
884 PyErr_Clear();
885 }
886 break;
887 }
888
889 /* Interpret it as an int (__index__) */
Georg Brandl3e758462008-07-16 23:10:05 +0000890 rc = _getbytevalue(item, &value);
Christian Heimes44720832008-05-26 13:01:01 +0000891 Py_DECREF(item);
Georg Brandl3e758462008-07-16 23:10:05 +0000892 if (!rc)
Christian Heimes44720832008-05-26 13:01:01 +0000893 goto error;
894
Christian Heimes44720832008-05-26 13:01:01 +0000895 /* Append the byte */
896 if (Py_SIZE(self) < self->ob_alloc)
897 Py_SIZE(self)++;
898 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
899 goto error;
900 self->ob_bytes[Py_SIZE(self)-1] = value;
901 }
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 *
916bytes_repr(PyByteArrayObject *self)
917{
918 static const char *hexdigits = "0123456789abcdef";
919 const char *quote_prefix = "bytearray(b";
920 const char *quote_postfix = ")";
921 Py_ssize_t length = Py_SIZE(self);
922 /* 14 == strlen(quote_prefix) + 2 + strlen(quote_postfix) */
923 size_t newsize = 14 + 4 * length;
924 PyObject *v;
925 if (newsize > PY_SSIZE_T_MAX || newsize / 4 - 3 != length) {
926 PyErr_SetString(PyExc_OverflowError,
927 "bytearray object is too large to make repr");
928 return NULL;
929 }
930 v = PyUnicode_FromUnicode(NULL, newsize);
931 if (v == NULL) {
932 return NULL;
933 }
934 else {
935 register Py_ssize_t i;
936 register Py_UNICODE c;
937 register Py_UNICODE *p;
938 int quote;
939
940 /* Figure out which quote to use; single is preferred */
941 quote = '\'';
942 {
943 char *test, *start;
944 start = PyByteArray_AS_STRING(self);
945 for (test = start; test < start+length; ++test) {
946 if (*test == '"') {
947 quote = '\''; /* back to single */
948 goto decided;
949 }
950 else if (*test == '\'')
951 quote = '"';
952 }
953 decided:
954 ;
955 }
956
957 p = PyUnicode_AS_UNICODE(v);
958 while (*quote_prefix)
959 *p++ = *quote_prefix++;
960 *p++ = quote;
961
962 for (i = 0; i < length; i++) {
963 /* There's at least enough room for a hex escape
964 and a closing quote. */
965 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
966 c = self->ob_bytes[i];
967 if (c == '\'' || c == '\\')
968 *p++ = '\\', *p++ = c;
969 else if (c == '\t')
970 *p++ = '\\', *p++ = 't';
971 else if (c == '\n')
972 *p++ = '\\', *p++ = 'n';
973 else if (c == '\r')
974 *p++ = '\\', *p++ = 'r';
975 else if (c == 0)
976 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
977 else if (c < ' ' || c >= 0x7f) {
978 *p++ = '\\';
979 *p++ = 'x';
980 *p++ = hexdigits[(c & 0xf0) >> 4];
981 *p++ = hexdigits[c & 0xf];
982 }
983 else
984 *p++ = c;
985 }
986 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
987 *p++ = quote;
988 while (*quote_postfix) {
989 *p++ = *quote_postfix++;
990 }
991 *p = '\0';
992 if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
993 Py_DECREF(v);
994 return NULL;
995 }
996 return v;
997 }
998}
999
1000static PyObject *
1001bytes_str(PyObject *op)
1002{
1003#if 0
1004 if (Py_BytesWarningFlag) {
1005 if (PyErr_WarnEx(PyExc_BytesWarning,
1006 "str() on a bytearray instance", 1))
1007 return NULL;
1008 }
1009 return bytes_repr((PyByteArrayObject*)op);
1010#endif
1011 return PyBytes_FromStringAndSize(((PyByteArrayObject*)op)->ob_bytes, Py_SIZE(op));
1012}
1013
1014static PyObject *
1015bytes_richcompare(PyObject *self, PyObject *other, int op)
1016{
1017 Py_ssize_t self_size, other_size;
1018 Py_buffer self_bytes, other_bytes;
1019 PyObject *res;
1020 Py_ssize_t minsize;
1021 int cmp;
1022
1023 /* Bytes can be compared to anything that supports the (binary)
1024 buffer API. Except that a comparison with Unicode is always an
1025 error, even if the comparison is for equality. */
1026 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
1027 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
1028 if (Py_BytesWarningFlag && op == Py_EQ) {
1029 if (PyErr_WarnEx(PyExc_BytesWarning,
Ezio Melotti262c3ce2010-01-14 11:39:50 +00001030 "Comparison between bytearray and string", 1))
Christian Heimes44720832008-05-26 13:01:01 +00001031 return NULL;
1032 }
1033
1034 Py_INCREF(Py_NotImplemented);
1035 return Py_NotImplemented;
1036 }
1037
1038 self_size = _getbuffer(self, &self_bytes);
1039 if (self_size < 0) {
1040 PyErr_Clear();
1041 Py_INCREF(Py_NotImplemented);
1042 return Py_NotImplemented;
1043 }
1044
1045 other_size = _getbuffer(other, &other_bytes);
1046 if (other_size < 0) {
1047 PyErr_Clear();
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001048 PyBuffer_Release(&self_bytes);
Christian Heimes44720832008-05-26 13:01:01 +00001049 Py_INCREF(Py_NotImplemented);
1050 return Py_NotImplemented;
1051 }
1052
1053 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1054 /* Shortcut: if the lengths differ, the objects differ */
1055 cmp = (op == Py_NE);
1056 }
1057 else {
1058 minsize = self_size;
1059 if (other_size < minsize)
1060 minsize = other_size;
1061
1062 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1063 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1064
1065 if (cmp == 0) {
1066 if (self_size < other_size)
1067 cmp = -1;
1068 else if (self_size > other_size)
1069 cmp = 1;
1070 }
1071
1072 switch (op) {
1073 case Py_LT: cmp = cmp < 0; break;
1074 case Py_LE: cmp = cmp <= 0; break;
1075 case Py_EQ: cmp = cmp == 0; break;
1076 case Py_NE: cmp = cmp != 0; break;
1077 case Py_GT: cmp = cmp > 0; break;
1078 case Py_GE: cmp = cmp >= 0; break;
1079 }
1080 }
1081
1082 res = cmp ? Py_True : Py_False;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001083 PyBuffer_Release(&self_bytes);
1084 PyBuffer_Release(&other_bytes);
Christian Heimes44720832008-05-26 13:01:01 +00001085 Py_INCREF(res);
1086 return res;
1087}
1088
1089static void
1090bytes_dealloc(PyByteArrayObject *self)
1091{
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001092 if (self->ob_exports > 0) {
1093 PyErr_SetString(PyExc_SystemError,
Georg Brandle9b91212009-04-05 21:26:31 +00001094 "deallocated bytearray object has exported buffers");
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001095 PyErr_Print();
1096 }
Christian Heimes44720832008-05-26 13:01:01 +00001097 if (self->ob_bytes != 0) {
1098 PyMem_Free(self->ob_bytes);
1099 }
1100 Py_TYPE(self)->tp_free((PyObject *)self);
1101}
1102
1103
1104/* -------------------------------------------------------------------- */
1105/* Methods */
1106
1107#define STRINGLIB_CHAR char
1108#define STRINGLIB_CMP memcmp
1109#define STRINGLIB_LEN PyByteArray_GET_SIZE
1110#define STRINGLIB_STR PyByteArray_AS_STRING
1111#define STRINGLIB_NEW PyByteArray_FromStringAndSize
1112#define STRINGLIB_EMPTY nullbytes
1113#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1114#define STRINGLIB_MUTABLE 1
Christian Heimes7d4c3172008-08-22 19:47:25 +00001115#define FROM_BYTEARRAY 1
Christian Heimes44720832008-05-26 13:01:01 +00001116
1117#include "stringlib/fastsearch.h"
1118#include "stringlib/count.h"
1119#include "stringlib/find.h"
1120#include "stringlib/partition.h"
1121#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
1128Py_LOCAL_INLINE(void)
1129_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
1130{
1131 if (*end > len)
1132 *end = len;
1133 else if (*end < 0)
1134 *end += len;
1135 if (*end < 0)
1136 *end = 0;
1137 if (*start < 0)
1138 *start += len;
1139 if (*start < 0)
1140 *start = 0;
1141}
1142
1143
1144Py_LOCAL_INLINE(Py_ssize_t)
1145bytes_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
1146{
1147 PyObject *subobj;
1148 Py_buffer subbuf;
1149 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1150 Py_ssize_t res;
1151
1152 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj,
1153 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1154 return -2;
1155 if (_getbuffer(subobj, &subbuf) < 0)
1156 return -2;
1157 if (dir > 0)
1158 res = stringlib_find_slice(
1159 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1160 subbuf.buf, subbuf.len, start, end);
1161 else
1162 res = stringlib_rfind_slice(
1163 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1164 subbuf.buf, subbuf.len, start, end);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001165 PyBuffer_Release(&subbuf);
Christian Heimes44720832008-05-26 13:01:01 +00001166 return res;
1167}
1168
1169PyDoc_STRVAR(find__doc__,
1170"B.find(sub [,start [,end]]) -> int\n\
1171\n\
1172Return the lowest index in B where subsection sub is found,\n\
1173such that sub is contained within s[start,end]. Optional\n\
1174arguments start and end are interpreted as in slice notation.\n\
1175\n\
1176Return -1 on failure.");
1177
1178static PyObject *
1179bytes_find(PyByteArrayObject *self, PyObject *args)
1180{
1181 Py_ssize_t result = bytes_find_internal(self, args, +1);
1182 if (result == -2)
1183 return NULL;
1184 return PyInt_FromSsize_t(result);
1185}
1186
1187PyDoc_STRVAR(count__doc__,
1188"B.count(sub [,start [,end]]) -> int\n\
1189\n\
1190Return the number of non-overlapping occurrences of subsection sub in\n\
1191bytes B[start:end]. Optional arguments start and end are interpreted\n\
1192as in slice notation.");
1193
1194static PyObject *
1195bytes_count(PyByteArrayObject *self, PyObject *args)
1196{
1197 PyObject *sub_obj;
1198 const char *str = PyByteArray_AS_STRING(self);
1199 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
1200 Py_buffer vsub;
1201 PyObject *count_obj;
1202
1203 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
1204 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1205 return NULL;
1206
1207 if (_getbuffer(sub_obj, &vsub) < 0)
1208 return NULL;
1209
1210 _adjust_indices(&start, &end, PyByteArray_GET_SIZE(self));
1211
1212 count_obj = PyInt_FromSsize_t(
1213 stringlib_count(str + start, end - start, vsub.buf, vsub.len)
1214 );
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001215 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00001216 return count_obj;
1217}
1218
1219
1220PyDoc_STRVAR(index__doc__,
1221"B.index(sub [,start [,end]]) -> int\n\
1222\n\
1223Like B.find() but raise ValueError when the subsection is not found.");
1224
1225static PyObject *
1226bytes_index(PyByteArrayObject *self, PyObject *args)
1227{
1228 Py_ssize_t result = bytes_find_internal(self, args, +1);
1229 if (result == -2)
1230 return NULL;
1231 if (result == -1) {
1232 PyErr_SetString(PyExc_ValueError,
1233 "subsection not found");
1234 return NULL;
1235 }
1236 return PyInt_FromSsize_t(result);
1237}
1238
1239
1240PyDoc_STRVAR(rfind__doc__,
1241"B.rfind(sub [,start [,end]]) -> int\n\
1242\n\
1243Return the highest index in B where subsection sub is found,\n\
1244such that sub is contained within s[start,end]. Optional\n\
1245arguments start and end are interpreted as in slice notation.\n\
1246\n\
1247Return -1 on failure.");
1248
1249static PyObject *
1250bytes_rfind(PyByteArrayObject *self, PyObject *args)
1251{
1252 Py_ssize_t result = bytes_find_internal(self, args, -1);
1253 if (result == -2)
1254 return NULL;
1255 return PyInt_FromSsize_t(result);
1256}
1257
1258
1259PyDoc_STRVAR(rindex__doc__,
1260"B.rindex(sub [,start [,end]]) -> int\n\
1261\n\
1262Like B.rfind() but raise ValueError when the subsection is not found.");
1263
1264static PyObject *
1265bytes_rindex(PyByteArrayObject *self, PyObject *args)
1266{
1267 Py_ssize_t result = bytes_find_internal(self, args, -1);
1268 if (result == -2)
1269 return NULL;
1270 if (result == -1) {
1271 PyErr_SetString(PyExc_ValueError,
1272 "subsection not found");
1273 return NULL;
1274 }
1275 return PyInt_FromSsize_t(result);
1276}
1277
1278
1279static int
1280bytes_contains(PyObject *self, PyObject *arg)
1281{
1282 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1283 if (ival == -1 && PyErr_Occurred()) {
1284 Py_buffer varg;
1285 int pos;
1286 PyErr_Clear();
1287 if (_getbuffer(arg, &varg) < 0)
1288 return -1;
1289 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1290 varg.buf, varg.len, 0);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001291 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00001292 return pos >= 0;
1293 }
1294 if (ival < 0 || ival >= 256) {
1295 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1296 return -1;
1297 }
1298
1299 return memchr(PyByteArray_AS_STRING(self), ival, Py_SIZE(self)) != NULL;
1300}
1301
1302
1303/* Matches the end (direction >= 0) or start (direction < 0) of self
1304 * against substr, using the start and end arguments. Returns
1305 * -1 on error, 0 if not found and 1 if found.
1306 */
1307Py_LOCAL(int)
1308_bytes_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
1309 Py_ssize_t end, int direction)
1310{
1311 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1312 const char* str;
1313 Py_buffer vsubstr;
1314 int rv = 0;
1315
1316 str = PyByteArray_AS_STRING(self);
1317
1318 if (_getbuffer(substr, &vsubstr) < 0)
1319 return -1;
1320
1321 _adjust_indices(&start, &end, len);
1322
1323 if (direction < 0) {
1324 /* startswith */
1325 if (start+vsubstr.len > len) {
1326 goto done;
1327 }
1328 } else {
1329 /* endswith */
1330 if (end-start < vsubstr.len || start > len) {
1331 goto done;
1332 }
1333
1334 if (end-vsubstr.len > start)
1335 start = end - vsubstr.len;
1336 }
1337 if (end-start >= vsubstr.len)
1338 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1339
1340done:
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001341 PyBuffer_Release(&vsubstr);
Christian Heimes44720832008-05-26 13:01:01 +00001342 return rv;
1343}
1344
1345
1346PyDoc_STRVAR(startswith__doc__,
1347"B.startswith(prefix [,start [,end]]) -> bool\n\
1348\n\
1349Return True if B starts with the specified prefix, False otherwise.\n\
1350With optional start, test B beginning at that position.\n\
1351With optional end, stop comparing B at that position.\n\
1352prefix can also be a tuple of strings to try.");
1353
1354static PyObject *
1355bytes_startswith(PyByteArrayObject *self, PyObject *args)
1356{
1357 Py_ssize_t start = 0;
1358 Py_ssize_t end = PY_SSIZE_T_MAX;
1359 PyObject *subobj;
1360 int result;
1361
1362 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
1363 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1364 return NULL;
1365 if (PyTuple_Check(subobj)) {
1366 Py_ssize_t i;
1367 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1368 result = _bytes_tailmatch(self,
1369 PyTuple_GET_ITEM(subobj, i),
1370 start, end, -1);
1371 if (result == -1)
1372 return NULL;
1373 else if (result) {
1374 Py_RETURN_TRUE;
1375 }
1376 }
1377 Py_RETURN_FALSE;
1378 }
1379 result = _bytes_tailmatch(self, subobj, start, end, -1);
1380 if (result == -1)
1381 return NULL;
1382 else
1383 return PyBool_FromLong(result);
1384}
1385
1386PyDoc_STRVAR(endswith__doc__,
1387"B.endswith(suffix [,start [,end]]) -> bool\n\
1388\n\
1389Return True if B ends with the specified suffix, False otherwise.\n\
1390With optional start, test B beginning at that position.\n\
1391With optional end, stop comparing B at that position.\n\
1392suffix can also be a tuple of strings to try.");
1393
1394static PyObject *
1395bytes_endswith(PyByteArrayObject *self, PyObject *args)
1396{
1397 Py_ssize_t start = 0;
1398 Py_ssize_t end = PY_SSIZE_T_MAX;
1399 PyObject *subobj;
1400 int result;
1401
1402 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
1403 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1404 return NULL;
1405 if (PyTuple_Check(subobj)) {
1406 Py_ssize_t i;
1407 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1408 result = _bytes_tailmatch(self,
1409 PyTuple_GET_ITEM(subobj, i),
1410 start, end, +1);
1411 if (result == -1)
1412 return NULL;
1413 else if (result) {
1414 Py_RETURN_TRUE;
1415 }
1416 }
1417 Py_RETURN_FALSE;
1418 }
1419 result = _bytes_tailmatch(self, subobj, start, end, +1);
1420 if (result == -1)
1421 return NULL;
1422 else
1423 return PyBool_FromLong(result);
1424}
1425
1426
1427PyDoc_STRVAR(translate__doc__,
1428"B.translate(table[, deletechars]) -> bytearray\n\
1429\n\
1430Return a copy of B, where all characters occurring in the\n\
1431optional argument deletechars are removed, and the remaining\n\
1432characters have been mapped through the given translation\n\
1433table, which must be a bytes object of length 256.");
1434
1435static PyObject *
1436bytes_translate(PyByteArrayObject *self, PyObject *args)
1437{
1438 register char *input, *output;
1439 register const char *table;
Benjamin Peterson866eba92008-11-19 22:05:53 +00001440 register Py_ssize_t i, c;
Christian Heimes44720832008-05-26 13:01:01 +00001441 PyObject *input_obj = (PyObject*)self;
1442 const char *output_start;
1443 Py_ssize_t inlen;
1444 PyObject *result;
1445 int trans_table[256];
1446 PyObject *tableobj, *delobj = NULL;
1447 Py_buffer vtable, vdel;
1448
1449 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
1450 &tableobj, &delobj))
1451 return NULL;
1452
1453 if (_getbuffer(tableobj, &vtable) < 0)
1454 return NULL;
1455
1456 if (vtable.len != 256) {
1457 PyErr_SetString(PyExc_ValueError,
1458 "translation table must be 256 characters long");
Georg Brandl11a81b22009-07-22 12:03:09 +00001459 PyBuffer_Release(&vtable);
1460 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00001461 }
1462
1463 if (delobj != NULL) {
1464 if (_getbuffer(delobj, &vdel) < 0) {
Georg Brandl11a81b22009-07-22 12:03:09 +00001465 PyBuffer_Release(&vtable);
1466 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00001467 }
1468 }
1469 else {
1470 vdel.buf = NULL;
1471 vdel.len = 0;
1472 }
1473
1474 table = (const char *)vtable.buf;
1475 inlen = PyByteArray_GET_SIZE(input_obj);
1476 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1477 if (result == NULL)
1478 goto done;
1479 output_start = output = PyByteArray_AsString(result);
1480 input = PyByteArray_AS_STRING(input_obj);
1481
1482 if (vdel.len == 0) {
1483 /* If no deletions are required, use faster code */
1484 for (i = inlen; --i >= 0; ) {
1485 c = Py_CHARMASK(*input++);
Benjamin Peterson866eba92008-11-19 22:05:53 +00001486 *output++ = table[c];
Christian Heimes44720832008-05-26 13:01:01 +00001487 }
Christian Heimes44720832008-05-26 13:01:01 +00001488 goto done;
1489 }
Antoine Pitrou599db7f2008-12-07 00:07:51 +00001490
Christian Heimes44720832008-05-26 13:01:01 +00001491 for (i = 0; i < 256; i++)
1492 trans_table[i] = Py_CHARMASK(table[i]);
1493
1494 for (i = 0; i < vdel.len; i++)
1495 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1496
1497 for (i = inlen; --i >= 0; ) {
1498 c = Py_CHARMASK(*input++);
1499 if (trans_table[c] != -1)
1500 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1501 continue;
Christian Heimes44720832008-05-26 13:01:01 +00001502 }
1503 /* Fix the size of the resulting string */
1504 if (inlen > 0)
1505 PyByteArray_Resize(result, output - output_start);
1506
1507done:
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001508 PyBuffer_Release(&vtable);
Christian Heimes44720832008-05-26 13:01:01 +00001509 if (delobj != NULL)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001510 PyBuffer_Release(&vdel);
Christian Heimes44720832008-05-26 13:01:01 +00001511 return result;
1512}
1513
1514
1515#define FORWARD 1
1516#define REVERSE -1
1517
1518/* find and count characters and substrings */
1519
1520#define findchar(target, target_len, c) \
1521 ((char *)memchr((const void *)(target), c, target_len))
1522
1523/* Don't call if length < 2 */
1524#define Py_STRING_MATCH(target, offset, pattern, length) \
1525 (target[offset] == pattern[0] && \
1526 target[offset+length-1] == pattern[length-1] && \
1527 !memcmp(target+offset+1, pattern+1, length-2) )
1528
1529
Benjamin Peterson866eba92008-11-19 22:05:53 +00001530/* Bytes ops must return a string, create a copy */
Christian Heimes44720832008-05-26 13:01:01 +00001531Py_LOCAL(PyByteArrayObject *)
1532return_self(PyByteArrayObject *self)
1533{
Christian Heimes44720832008-05-26 13:01:01 +00001534 return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1535 PyByteArray_AS_STRING(self),
1536 PyByteArray_GET_SIZE(self));
1537}
1538
1539Py_LOCAL_INLINE(Py_ssize_t)
1540countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1541{
1542 Py_ssize_t count=0;
1543 const char *start=target;
1544 const char *end=target+target_len;
1545
1546 while ( (start=findchar(start, end-start, c)) != NULL ) {
1547 count++;
1548 if (count >= maxcount)
1549 break;
1550 start += 1;
1551 }
1552 return count;
1553}
1554
1555Py_LOCAL(Py_ssize_t)
1556findstring(const char *target, Py_ssize_t target_len,
1557 const char *pattern, Py_ssize_t pattern_len,
1558 Py_ssize_t start,
1559 Py_ssize_t end,
1560 int direction)
1561{
1562 if (start < 0) {
1563 start += target_len;
1564 if (start < 0)
1565 start = 0;
1566 }
1567 if (end > target_len) {
1568 end = target_len;
1569 } else if (end < 0) {
1570 end += target_len;
1571 if (end < 0)
1572 end = 0;
1573 }
1574
1575 /* zero-length substrings always match at the first attempt */
1576 if (pattern_len == 0)
1577 return (direction > 0) ? start : end;
1578
1579 end -= pattern_len;
1580
1581 if (direction < 0) {
1582 for (; end >= start; end--)
1583 if (Py_STRING_MATCH(target, end, pattern, pattern_len))
1584 return end;
1585 } else {
1586 for (; start <= end; start++)
1587 if (Py_STRING_MATCH(target, start, pattern, pattern_len))
1588 return start;
1589 }
1590 return -1;
1591}
1592
1593Py_LOCAL_INLINE(Py_ssize_t)
1594countstring(const char *target, Py_ssize_t target_len,
1595 const char *pattern, Py_ssize_t pattern_len,
1596 Py_ssize_t start,
1597 Py_ssize_t end,
1598 int direction, Py_ssize_t maxcount)
1599{
1600 Py_ssize_t count=0;
1601
1602 if (start < 0) {
1603 start += target_len;
1604 if (start < 0)
1605 start = 0;
1606 }
1607 if (end > target_len) {
1608 end = target_len;
1609 } else if (end < 0) {
1610 end += target_len;
1611 if (end < 0)
1612 end = 0;
1613 }
1614
1615 /* zero-length substrings match everywhere */
1616 if (pattern_len == 0 || maxcount == 0) {
1617 if (target_len+1 < maxcount)
1618 return target_len+1;
1619 return maxcount;
1620 }
1621
1622 end -= pattern_len;
1623 if (direction < 0) {
1624 for (; (end >= start); end--)
1625 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) {
1626 count++;
1627 if (--maxcount <= 0) break;
1628 end -= pattern_len-1;
1629 }
1630 } else {
1631 for (; (start <= end); start++)
1632 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) {
1633 count++;
1634 if (--maxcount <= 0)
1635 break;
1636 start += pattern_len-1;
1637 }
1638 }
1639 return count;
1640}
1641
1642
1643/* Algorithms for different cases of string replacement */
1644
1645/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1646Py_LOCAL(PyByteArrayObject *)
1647replace_interleave(PyByteArrayObject *self,
1648 const char *to_s, Py_ssize_t to_len,
1649 Py_ssize_t maxcount)
1650{
1651 char *self_s, *result_s;
1652 Py_ssize_t self_len, result_len;
1653 Py_ssize_t count, i, product;
1654 PyByteArrayObject *result;
1655
1656 self_len = PyByteArray_GET_SIZE(self);
1657
1658 /* 1 at the end plus 1 after every character */
1659 count = self_len+1;
1660 if (maxcount < count)
1661 count = maxcount;
1662
1663 /* Check for overflow */
1664 /* result_len = count * to_len + self_len; */
1665 product = count * to_len;
1666 if (product / to_len != count) {
1667 PyErr_SetString(PyExc_OverflowError,
1668 "replace string is too long");
1669 return NULL;
1670 }
1671 result_len = product + self_len;
1672 if (result_len < 0) {
1673 PyErr_SetString(PyExc_OverflowError,
1674 "replace string is too long");
1675 return NULL;
1676 }
1677
1678 if (! (result = (PyByteArrayObject *)
1679 PyByteArray_FromStringAndSize(NULL, result_len)) )
1680 return NULL;
1681
1682 self_s = PyByteArray_AS_STRING(self);
1683 result_s = PyByteArray_AS_STRING(result);
1684
1685 /* TODO: special case single character, which doesn't need memcpy */
1686
1687 /* Lay the first one down (guaranteed this will occur) */
1688 Py_MEMCPY(result_s, to_s, to_len);
1689 result_s += to_len;
1690 count -= 1;
1691
1692 for (i=0; i<count; i++) {
1693 *result_s++ = *self_s++;
1694 Py_MEMCPY(result_s, to_s, to_len);
1695 result_s += to_len;
1696 }
1697
1698 /* Copy the rest of the original string */
1699 Py_MEMCPY(result_s, self_s, self_len-i);
1700
1701 return result;
1702}
1703
1704/* Special case for deleting a single character */
1705/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1706Py_LOCAL(PyByteArrayObject *)
1707replace_delete_single_character(PyByteArrayObject *self,
1708 char from_c, Py_ssize_t maxcount)
1709{
1710 char *self_s, *result_s;
1711 char *start, *next, *end;
1712 Py_ssize_t self_len, result_len;
1713 Py_ssize_t count;
1714 PyByteArrayObject *result;
1715
1716 self_len = PyByteArray_GET_SIZE(self);
1717 self_s = PyByteArray_AS_STRING(self);
1718
1719 count = countchar(self_s, self_len, from_c, maxcount);
1720 if (count == 0) {
1721 return return_self(self);
1722 }
1723
1724 result_len = self_len - count; /* from_len == 1 */
1725 assert(result_len>=0);
1726
1727 if ( (result = (PyByteArrayObject *)
1728 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1729 return NULL;
1730 result_s = PyByteArray_AS_STRING(result);
1731
1732 start = self_s;
1733 end = self_s + self_len;
1734 while (count-- > 0) {
1735 next = findchar(start, end-start, from_c);
1736 if (next == NULL)
1737 break;
1738 Py_MEMCPY(result_s, start, next-start);
1739 result_s += (next-start);
1740 start = next+1;
1741 }
1742 Py_MEMCPY(result_s, start, end-start);
1743
1744 return result;
1745}
1746
1747/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1748
1749Py_LOCAL(PyByteArrayObject *)
1750replace_delete_substring(PyByteArrayObject *self,
1751 const char *from_s, Py_ssize_t from_len,
1752 Py_ssize_t maxcount)
1753{
1754 char *self_s, *result_s;
1755 char *start, *next, *end;
1756 Py_ssize_t self_len, result_len;
1757 Py_ssize_t count, offset;
1758 PyByteArrayObject *result;
1759
1760 self_len = PyByteArray_GET_SIZE(self);
1761 self_s = PyByteArray_AS_STRING(self);
1762
1763 count = countstring(self_s, self_len,
1764 from_s, from_len,
1765 0, self_len, 1,
1766 maxcount);
1767
1768 if (count == 0) {
1769 /* no matches */
1770 return return_self(self);
1771 }
1772
1773 result_len = self_len - (count * from_len);
1774 assert (result_len>=0);
1775
1776 if ( (result = (PyByteArrayObject *)
1777 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL )
1778 return NULL;
1779
1780 result_s = PyByteArray_AS_STRING(result);
1781
1782 start = self_s;
1783 end = self_s + self_len;
1784 while (count-- > 0) {
1785 offset = findstring(start, end-start,
1786 from_s, from_len,
1787 0, end-start, FORWARD);
1788 if (offset == -1)
1789 break;
1790 next = start + offset;
1791
1792 Py_MEMCPY(result_s, start, next-start);
1793
1794 result_s += (next-start);
1795 start = next+from_len;
1796 }
1797 Py_MEMCPY(result_s, start, end-start);
1798 return result;
1799}
1800
1801/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1802Py_LOCAL(PyByteArrayObject *)
1803replace_single_character_in_place(PyByteArrayObject *self,
1804 char from_c, char to_c,
1805 Py_ssize_t maxcount)
1806{
1807 char *self_s, *result_s, *start, *end, *next;
1808 Py_ssize_t self_len;
1809 PyByteArrayObject *result;
1810
1811 /* The result string will be the same size */
1812 self_s = PyByteArray_AS_STRING(self);
1813 self_len = PyByteArray_GET_SIZE(self);
1814
1815 next = findchar(self_s, self_len, from_c);
1816
1817 if (next == NULL) {
1818 /* No matches; return the original bytes */
1819 return return_self(self);
1820 }
1821
1822 /* Need to make a new bytes */
1823 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1824 if (result == NULL)
1825 return NULL;
1826 result_s = PyByteArray_AS_STRING(result);
1827 Py_MEMCPY(result_s, self_s, self_len);
1828
1829 /* change everything in-place, starting with this one */
1830 start = result_s + (next-self_s);
1831 *start = to_c;
1832 start++;
1833 end = result_s + self_len;
1834
1835 while (--maxcount > 0) {
1836 next = findchar(start, end-start, from_c);
1837 if (next == NULL)
1838 break;
1839 *next = to_c;
1840 start = next+1;
1841 }
1842
1843 return result;
1844}
1845
1846/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1847Py_LOCAL(PyByteArrayObject *)
1848replace_substring_in_place(PyByteArrayObject *self,
1849 const char *from_s, Py_ssize_t from_len,
1850 const char *to_s, Py_ssize_t to_len,
1851 Py_ssize_t maxcount)
1852{
1853 char *result_s, *start, *end;
1854 char *self_s;
1855 Py_ssize_t self_len, offset;
1856 PyByteArrayObject *result;
1857
1858 /* The result bytes will be the same size */
1859
1860 self_s = PyByteArray_AS_STRING(self);
1861 self_len = PyByteArray_GET_SIZE(self);
1862
1863 offset = findstring(self_s, self_len,
1864 from_s, from_len,
1865 0, self_len, FORWARD);
1866 if (offset == -1) {
1867 /* No matches; return the original bytes */
1868 return return_self(self);
1869 }
1870
1871 /* Need to make a new bytes */
1872 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1873 if (result == NULL)
1874 return NULL;
1875 result_s = PyByteArray_AS_STRING(result);
1876 Py_MEMCPY(result_s, self_s, self_len);
1877
1878 /* change everything in-place, starting with this one */
1879 start = result_s + offset;
1880 Py_MEMCPY(start, to_s, from_len);
1881 start += from_len;
1882 end = result_s + self_len;
1883
1884 while ( --maxcount > 0) {
1885 offset = findstring(start, end-start,
1886 from_s, from_len,
1887 0, end-start, FORWARD);
1888 if (offset==-1)
1889 break;
1890 Py_MEMCPY(start+offset, to_s, from_len);
1891 start += offset+from_len;
1892 }
1893
1894 return result;
1895}
1896
1897/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
1898Py_LOCAL(PyByteArrayObject *)
1899replace_single_character(PyByteArrayObject *self,
1900 char from_c,
1901 const char *to_s, Py_ssize_t to_len,
1902 Py_ssize_t maxcount)
1903{
1904 char *self_s, *result_s;
1905 char *start, *next, *end;
1906 Py_ssize_t self_len, result_len;
1907 Py_ssize_t count, product;
1908 PyByteArrayObject *result;
1909
1910 self_s = PyByteArray_AS_STRING(self);
1911 self_len = PyByteArray_GET_SIZE(self);
1912
1913 count = countchar(self_s, self_len, from_c, maxcount);
1914 if (count == 0) {
1915 /* no matches, return unchanged */
1916 return return_self(self);
1917 }
1918
1919 /* use the difference between current and new, hence the "-1" */
1920 /* result_len = self_len + count * (to_len-1) */
1921 product = count * (to_len-1);
1922 if (product / (to_len-1) != count) {
1923 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1924 return NULL;
1925 }
1926 result_len = self_len + product;
1927 if (result_len < 0) {
1928 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1929 return NULL;
1930 }
1931
1932 if ( (result = (PyByteArrayObject *)
1933 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1934 return NULL;
1935 result_s = PyByteArray_AS_STRING(result);
1936
1937 start = self_s;
1938 end = self_s + self_len;
1939 while (count-- > 0) {
1940 next = findchar(start, end-start, from_c);
1941 if (next == NULL)
1942 break;
1943
1944 if (next == start) {
1945 /* replace with the 'to' */
1946 Py_MEMCPY(result_s, to_s, to_len);
1947 result_s += to_len;
1948 start += 1;
1949 } else {
1950 /* copy the unchanged old then the 'to' */
1951 Py_MEMCPY(result_s, start, next-start);
1952 result_s += (next-start);
1953 Py_MEMCPY(result_s, to_s, to_len);
1954 result_s += to_len;
1955 start = next+1;
1956 }
1957 }
1958 /* Copy the remainder of the remaining bytes */
1959 Py_MEMCPY(result_s, start, end-start);
1960
1961 return result;
1962}
1963
1964/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
1965Py_LOCAL(PyByteArrayObject *)
1966replace_substring(PyByteArrayObject *self,
1967 const char *from_s, Py_ssize_t from_len,
1968 const char *to_s, Py_ssize_t to_len,
1969 Py_ssize_t maxcount)
1970{
1971 char *self_s, *result_s;
1972 char *start, *next, *end;
1973 Py_ssize_t self_len, result_len;
1974 Py_ssize_t count, offset, product;
1975 PyByteArrayObject *result;
1976
1977 self_s = PyByteArray_AS_STRING(self);
1978 self_len = PyByteArray_GET_SIZE(self);
1979
1980 count = countstring(self_s, self_len,
1981 from_s, from_len,
1982 0, self_len, FORWARD, maxcount);
1983 if (count == 0) {
1984 /* no matches, return unchanged */
1985 return return_self(self);
1986 }
1987
1988 /* Check for overflow */
1989 /* result_len = self_len + count * (to_len-from_len) */
1990 product = count * (to_len-from_len);
1991 if (product / (to_len-from_len) != count) {
1992 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1993 return NULL;
1994 }
1995 result_len = self_len + product;
1996 if (result_len < 0) {
1997 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1998 return NULL;
1999 }
2000
2001 if ( (result = (PyByteArrayObject *)
2002 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
2003 return NULL;
2004 result_s = PyByteArray_AS_STRING(result);
2005
2006 start = self_s;
2007 end = self_s + self_len;
2008 while (count-- > 0) {
2009 offset = findstring(start, end-start,
2010 from_s, from_len,
2011 0, end-start, FORWARD);
2012 if (offset == -1)
2013 break;
2014 next = start+offset;
2015 if (next == start) {
2016 /* replace with the 'to' */
2017 Py_MEMCPY(result_s, to_s, to_len);
2018 result_s += to_len;
2019 start += from_len;
2020 } else {
2021 /* copy the unchanged old then the 'to' */
2022 Py_MEMCPY(result_s, start, next-start);
2023 result_s += (next-start);
2024 Py_MEMCPY(result_s, to_s, to_len);
2025 result_s += to_len;
2026 start = next+from_len;
2027 }
2028 }
2029 /* Copy the remainder of the remaining bytes */
2030 Py_MEMCPY(result_s, start, end-start);
2031
2032 return result;
2033}
2034
2035
2036Py_LOCAL(PyByteArrayObject *)
2037replace(PyByteArrayObject *self,
2038 const char *from_s, Py_ssize_t from_len,
2039 const char *to_s, Py_ssize_t to_len,
2040 Py_ssize_t maxcount)
2041{
2042 if (maxcount < 0) {
2043 maxcount = PY_SSIZE_T_MAX;
2044 } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) {
2045 /* nothing to do; return the original bytes */
2046 return return_self(self);
2047 }
2048
2049 if (maxcount == 0 ||
2050 (from_len == 0 && to_len == 0)) {
2051 /* nothing to do; return the original bytes */
2052 return return_self(self);
2053 }
2054
2055 /* Handle zero-length special cases */
2056
2057 if (from_len == 0) {
2058 /* insert the 'to' bytes everywhere. */
2059 /* >>> "Python".replace("", ".") */
2060 /* '.P.y.t.h.o.n.' */
2061 return replace_interleave(self, to_s, to_len, maxcount);
2062 }
2063
2064 /* Except for "".replace("", "A") == "A" there is no way beyond this */
2065 /* point for an empty self bytes to generate a non-empty bytes */
2066 /* Special case so the remaining code always gets a non-empty bytes */
2067 if (PyByteArray_GET_SIZE(self) == 0) {
2068 return return_self(self);
2069 }
2070
2071 if (to_len == 0) {
2072 /* delete all occurances of 'from' bytes */
2073 if (from_len == 1) {
2074 return replace_delete_single_character(
2075 self, from_s[0], maxcount);
2076 } else {
2077 return replace_delete_substring(self, from_s, from_len, maxcount);
2078 }
2079 }
2080
2081 /* Handle special case where both bytes have the same length */
2082
2083 if (from_len == to_len) {
2084 if (from_len == 1) {
2085 return replace_single_character_in_place(
2086 self,
2087 from_s[0],
2088 to_s[0],
2089 maxcount);
2090 } else {
2091 return replace_substring_in_place(
2092 self, from_s, from_len, to_s, to_len, maxcount);
2093 }
2094 }
2095
2096 /* Otherwise use the more generic algorithms */
2097 if (from_len == 1) {
2098 return replace_single_character(self, from_s[0],
2099 to_s, to_len, maxcount);
2100 } else {
2101 /* len('from')>=2, len('to')>=1 */
2102 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
2103 }
2104}
2105
2106
2107PyDoc_STRVAR(replace__doc__,
2108"B.replace(old, new[, count]) -> bytes\n\
2109\n\
2110Return a copy of B with all occurrences of subsection\n\
2111old replaced by new. If the optional argument count is\n\
2112given, only the first count occurrences are replaced.");
2113
2114static PyObject *
2115bytes_replace(PyByteArrayObject *self, PyObject *args)
2116{
2117 Py_ssize_t count = -1;
2118 PyObject *from, *to, *res;
2119 Py_buffer vfrom, vto;
2120
2121 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
2122 return NULL;
2123
2124 if (_getbuffer(from, &vfrom) < 0)
2125 return NULL;
2126 if (_getbuffer(to, &vto) < 0) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002127 PyBuffer_Release(&vfrom);
Christian Heimes44720832008-05-26 13:01:01 +00002128 return NULL;
2129 }
2130
2131 res = (PyObject *)replace((PyByteArrayObject *) self,
2132 vfrom.buf, vfrom.len,
2133 vto.buf, vto.len, count);
2134
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002135 PyBuffer_Release(&vfrom);
2136 PyBuffer_Release(&vto);
Christian Heimes44720832008-05-26 13:01:01 +00002137 return res;
2138}
2139
2140
2141/* Overallocate the initial list to reduce the number of reallocs for small
2142 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three
2143 resizes, to sizes 4, 8, then 16. Most observed string splits are for human
2144 text (roughly 11 words per line) and field delimited data (usually 1-10
2145 fields). For large strings the split algorithms are bandwidth limited
2146 so increasing the preallocation likely will not improve things.*/
2147
2148#define MAX_PREALLOC 12
2149
2150/* 5 splits gives 6 elements */
2151#define PREALLOC_SIZE(maxsplit) \
2152 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
2153
2154#define SPLIT_APPEND(data, left, right) \
2155 str = PyByteArray_FromStringAndSize((data) + (left), \
2156 (right) - (left)); \
2157 if (str == NULL) \
2158 goto onError; \
2159 if (PyList_Append(list, str)) { \
2160 Py_DECREF(str); \
2161 goto onError; \
2162 } \
2163 else \
2164 Py_DECREF(str);
2165
2166#define SPLIT_ADD(data, left, right) { \
2167 str = PyByteArray_FromStringAndSize((data) + (left), \
2168 (right) - (left)); \
2169 if (str == NULL) \
2170 goto onError; \
2171 if (count < MAX_PREALLOC) { \
2172 PyList_SET_ITEM(list, count, str); \
2173 } else { \
2174 if (PyList_Append(list, str)) { \
2175 Py_DECREF(str); \
2176 goto onError; \
2177 } \
2178 else \
2179 Py_DECREF(str); \
2180 } \
2181 count++; }
2182
2183/* Always force the list to the expected size. */
2184#define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count
2185
2186
2187Py_LOCAL_INLINE(PyObject *)
2188split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
2189{
2190 register Py_ssize_t i, j, count = 0;
2191 PyObject *str;
2192 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
2193
2194 if (list == NULL)
2195 return NULL;
2196
2197 i = j = 0;
2198 while ((j < len) && (maxcount-- > 0)) {
2199 for(; j < len; j++) {
2200 /* I found that using memchr makes no difference */
2201 if (s[j] == ch) {
2202 SPLIT_ADD(s, i, j);
2203 i = j = j + 1;
2204 break;
2205 }
2206 }
2207 }
2208 if (i <= len) {
2209 SPLIT_ADD(s, i, len);
2210 }
2211 FIX_PREALLOC_SIZE(list);
2212 return list;
2213
2214 onError:
2215 Py_DECREF(list);
2216 return NULL;
2217}
2218
2219
2220Py_LOCAL_INLINE(PyObject *)
2221split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount)
2222{
2223 register Py_ssize_t i, j, count = 0;
2224 PyObject *str;
2225 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
2226
2227 if (list == NULL)
2228 return NULL;
2229
2230 for (i = j = 0; i < len; ) {
2231 /* find a token */
2232 while (i < len && ISSPACE(s[i]))
2233 i++;
2234 j = i;
2235 while (i < len && !ISSPACE(s[i]))
2236 i++;
2237 if (j < i) {
2238 if (maxcount-- <= 0)
2239 break;
2240 SPLIT_ADD(s, j, i);
2241 while (i < len && ISSPACE(s[i]))
2242 i++;
2243 j = i;
2244 }
2245 }
2246 if (j < len) {
2247 SPLIT_ADD(s, j, len);
2248 }
2249 FIX_PREALLOC_SIZE(list);
2250 return list;
2251
2252 onError:
2253 Py_DECREF(list);
2254 return NULL;
2255}
2256
2257PyDoc_STRVAR(split__doc__,
2258"B.split([sep[, maxsplit]]) -> list of bytearray\n\
2259\n\
2260Return a list of the sections in B, using sep as the delimiter.\n\
2261If sep is not given, B is split on ASCII whitespace characters\n\
2262(space, tab, return, newline, formfeed, vertical tab).\n\
2263If maxsplit is given, at most maxsplit splits are done.");
2264
2265static PyObject *
2266bytes_split(PyByteArrayObject *self, PyObject *args)
2267{
2268 Py_ssize_t len = PyByteArray_GET_SIZE(self), n, i, j;
2269 Py_ssize_t maxsplit = -1, count = 0;
2270 const char *s = PyByteArray_AS_STRING(self), *sub;
2271 PyObject *list, *str, *subobj = Py_None;
2272 Py_buffer vsub;
2273#ifdef USE_FAST
2274 Py_ssize_t pos;
2275#endif
2276
2277 if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit))
2278 return NULL;
2279 if (maxsplit < 0)
2280 maxsplit = PY_SSIZE_T_MAX;
2281
2282 if (subobj == Py_None)
2283 return split_whitespace(s, len, maxsplit);
2284
2285 if (_getbuffer(subobj, &vsub) < 0)
2286 return NULL;
2287 sub = vsub.buf;
2288 n = vsub.len;
2289
2290 if (n == 0) {
2291 PyErr_SetString(PyExc_ValueError, "empty separator");
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002292 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002293 return NULL;
2294 }
Amaury Forgeot d'Arc313bda12008-08-17 21:05:18 +00002295 if (n == 1) {
2296 list = split_char(s, len, sub[0], maxsplit);
2297 PyBuffer_Release(&vsub);
2298 return list;
2299 }
Christian Heimes44720832008-05-26 13:01:01 +00002300
2301 list = PyList_New(PREALLOC_SIZE(maxsplit));
2302 if (list == NULL) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002303 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002304 return NULL;
2305 }
2306
2307#ifdef USE_FAST
2308 i = j = 0;
2309 while (maxsplit-- > 0) {
2310 pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH);
2311 if (pos < 0)
2312 break;
2313 j = i+pos;
2314 SPLIT_ADD(s, i, j);
2315 i = j + n;
2316 }
2317#else
2318 i = j = 0;
2319 while ((j+n <= len) && (maxsplit-- > 0)) {
2320 for (; j+n <= len; j++) {
2321 if (Py_STRING_MATCH(s, j, sub, n)) {
2322 SPLIT_ADD(s, i, j);
2323 i = j = j + n;
2324 break;
2325 }
2326 }
2327 }
2328#endif
2329 SPLIT_ADD(s, i, len);
2330 FIX_PREALLOC_SIZE(list);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002331 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002332 return list;
2333
2334 onError:
2335 Py_DECREF(list);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002336 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002337 return NULL;
2338}
2339
2340/* stringlib's partition shares nullbytes in some cases.
2341 undo this, we don't want the nullbytes to be shared. */
2342static PyObject *
2343make_nullbytes_unique(PyObject *result)
2344{
2345 if (result != NULL) {
2346 int i;
2347 assert(PyTuple_Check(result));
2348 assert(PyTuple_GET_SIZE(result) == 3);
2349 for (i = 0; i < 3; i++) {
2350 if (PyTuple_GET_ITEM(result, i) == (PyObject *)nullbytes) {
2351 PyObject *new = PyByteArray_FromStringAndSize(NULL, 0);
2352 if (new == NULL) {
2353 Py_DECREF(result);
2354 result = NULL;
2355 break;
2356 }
2357 Py_DECREF(nullbytes);
2358 PyTuple_SET_ITEM(result, i, new);
2359 }
2360 }
2361 }
2362 return result;
2363}
2364
2365PyDoc_STRVAR(partition__doc__,
2366"B.partition(sep) -> (head, sep, tail)\n\
2367\n\
2368Searches for the separator sep in B, and returns the part before it,\n\
2369the separator itself, and the part after it. If the separator is not\n\
2370found, returns B and two empty bytearray objects.");
2371
2372static PyObject *
2373bytes_partition(PyByteArrayObject *self, PyObject *sep_obj)
2374{
2375 PyObject *bytesep, *result;
2376
2377 bytesep = PyByteArray_FromObject(sep_obj);
2378 if (! bytesep)
2379 return NULL;
2380
2381 result = stringlib_partition(
2382 (PyObject*) self,
2383 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2384 bytesep,
2385 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2386 );
2387
2388 Py_DECREF(bytesep);
2389 return make_nullbytes_unique(result);
2390}
2391
2392PyDoc_STRVAR(rpartition__doc__,
Ezio Melottidabb5f72010-01-25 11:46:11 +00002393"B.rpartition(sep) -> (head, sep, tail)\n\
Christian Heimes44720832008-05-26 13:01:01 +00002394\n\
2395Searches for the separator sep in B, starting at the end of B,\n\
2396and returns the part before it, the separator itself, and the\n\
2397part after it. If the separator is not found, returns two empty\n\
2398bytearray objects and B.");
2399
2400static PyObject *
2401bytes_rpartition(PyByteArrayObject *self, PyObject *sep_obj)
2402{
2403 PyObject *bytesep, *result;
2404
2405 bytesep = PyByteArray_FromObject(sep_obj);
2406 if (! bytesep)
2407 return NULL;
2408
2409 result = stringlib_rpartition(
2410 (PyObject*) self,
2411 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2412 bytesep,
2413 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2414 );
2415
2416 Py_DECREF(bytesep);
2417 return make_nullbytes_unique(result);
2418}
2419
2420Py_LOCAL_INLINE(PyObject *)
2421rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
2422{
2423 register Py_ssize_t i, j, count=0;
2424 PyObject *str;
2425 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
2426
2427 if (list == NULL)
2428 return NULL;
2429
2430 i = j = len - 1;
2431 while ((i >= 0) && (maxcount-- > 0)) {
2432 for (; i >= 0; i--) {
2433 if (s[i] == ch) {
2434 SPLIT_ADD(s, i + 1, j + 1);
2435 j = i = i - 1;
2436 break;
2437 }
2438 }
2439 }
2440 if (j >= -1) {
2441 SPLIT_ADD(s, 0, j + 1);
2442 }
2443 FIX_PREALLOC_SIZE(list);
2444 if (PyList_Reverse(list) < 0)
2445 goto onError;
2446
2447 return list;
2448
2449 onError:
2450 Py_DECREF(list);
2451 return NULL;
2452}
2453
2454Py_LOCAL_INLINE(PyObject *)
2455rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount)
2456{
2457 register Py_ssize_t i, j, count = 0;
2458 PyObject *str;
2459 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
2460
2461 if (list == NULL)
2462 return NULL;
2463
2464 for (i = j = len - 1; i >= 0; ) {
2465 /* find a token */
2466 while (i >= 0 && ISSPACE(s[i]))
2467 i--;
2468 j = i;
2469 while (i >= 0 && !ISSPACE(s[i]))
2470 i--;
2471 if (j > i) {
2472 if (maxcount-- <= 0)
2473 break;
2474 SPLIT_ADD(s, i + 1, j + 1);
2475 while (i >= 0 && ISSPACE(s[i]))
2476 i--;
2477 j = i;
2478 }
2479 }
2480 if (j >= 0) {
2481 SPLIT_ADD(s, 0, j + 1);
2482 }
2483 FIX_PREALLOC_SIZE(list);
2484 if (PyList_Reverse(list) < 0)
2485 goto onError;
2486
2487 return list;
2488
2489 onError:
2490 Py_DECREF(list);
2491 return NULL;
2492}
2493
2494PyDoc_STRVAR(rsplit__doc__,
2495"B.rsplit(sep[, maxsplit]) -> list of bytearray\n\
2496\n\
2497Return a list of the sections in B, using sep as the delimiter,\n\
2498starting at the end of B and working to the front.\n\
2499If sep is not given, B is split on ASCII whitespace characters\n\
2500(space, tab, return, newline, formfeed, vertical tab).\n\
2501If maxsplit is given, at most maxsplit splits are done.");
2502
2503static PyObject *
2504bytes_rsplit(PyByteArrayObject *self, PyObject *args)
2505{
2506 Py_ssize_t len = PyByteArray_GET_SIZE(self), n, i, j;
2507 Py_ssize_t maxsplit = -1, count = 0;
2508 const char *s = PyByteArray_AS_STRING(self), *sub;
2509 PyObject *list, *str, *subobj = Py_None;
2510 Py_buffer vsub;
2511
2512 if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit))
2513 return NULL;
2514 if (maxsplit < 0)
2515 maxsplit = PY_SSIZE_T_MAX;
2516
2517 if (subobj == Py_None)
2518 return rsplit_whitespace(s, len, maxsplit);
2519
2520 if (_getbuffer(subobj, &vsub) < 0)
2521 return NULL;
2522 sub = vsub.buf;
2523 n = vsub.len;
2524
2525 if (n == 0) {
2526 PyErr_SetString(PyExc_ValueError, "empty separator");
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002527 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002528 return NULL;
2529 }
Amaury Forgeot d'Arc313bda12008-08-17 21:05:18 +00002530 else if (n == 1) {
2531 list = rsplit_char(s, len, sub[0], maxsplit);
2532 PyBuffer_Release(&vsub);
2533 return list;
2534 }
Christian Heimes44720832008-05-26 13:01:01 +00002535
2536 list = PyList_New(PREALLOC_SIZE(maxsplit));
2537 if (list == NULL) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002538 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002539 return NULL;
2540 }
2541
2542 j = len;
2543 i = j - n;
2544
2545 while ( (i >= 0) && (maxsplit-- > 0) ) {
2546 for (; i>=0; i--) {
2547 if (Py_STRING_MATCH(s, i, sub, n)) {
2548 SPLIT_ADD(s, i + n, j);
2549 j = i;
2550 i -= n;
2551 break;
2552 }
2553 }
2554 }
2555 SPLIT_ADD(s, 0, j);
2556 FIX_PREALLOC_SIZE(list);
2557 if (PyList_Reverse(list) < 0)
2558 goto onError;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002559 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002560 return list;
2561
2562onError:
2563 Py_DECREF(list);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002564 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002565 return NULL;
2566}
2567
2568PyDoc_STRVAR(reverse__doc__,
2569"B.reverse() -> None\n\
2570\n\
2571Reverse the order of the values in B in place.");
2572static PyObject *
2573bytes_reverse(PyByteArrayObject *self, PyObject *unused)
2574{
2575 char swap, *head, *tail;
2576 Py_ssize_t i, j, n = Py_SIZE(self);
2577
2578 j = n / 2;
2579 head = self->ob_bytes;
2580 tail = head + n - 1;
2581 for (i = 0; i < j; i++) {
2582 swap = *head;
2583 *head++ = *tail;
2584 *tail-- = swap;
2585 }
2586
2587 Py_RETURN_NONE;
2588}
2589
2590PyDoc_STRVAR(insert__doc__,
2591"B.insert(index, int) -> None\n\
2592\n\
2593Insert a single item into the bytearray before the given index.");
2594static PyObject *
2595bytes_insert(PyByteArrayObject *self, PyObject *args)
2596{
Georg Brandl3e483f62008-07-16 22:57:41 +00002597 PyObject *value;
2598 int ival;
Christian Heimes44720832008-05-26 13:01:01 +00002599 Py_ssize_t where, n = Py_SIZE(self);
2600
Georg Brandl3e483f62008-07-16 22:57:41 +00002601 if (!PyArg_ParseTuple(args, "nO:insert", &where, &value))
Christian Heimes44720832008-05-26 13:01:01 +00002602 return NULL;
2603
2604 if (n == PY_SSIZE_T_MAX) {
2605 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson76e96432009-09-06 10:33:12 +00002606 "cannot add more objects to bytearray");
Christian Heimes44720832008-05-26 13:01:01 +00002607 return NULL;
2608 }
Georg Brandl3e483f62008-07-16 22:57:41 +00002609 if (!_getbytevalue(value, &ival))
Christian Heimes44720832008-05-26 13:01:01 +00002610 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00002611 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2612 return NULL;
2613
2614 if (where < 0) {
2615 where += n;
2616 if (where < 0)
2617 where = 0;
2618 }
2619 if (where > n)
2620 where = n;
2621 memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where);
Georg Brandl3e483f62008-07-16 22:57:41 +00002622 self->ob_bytes[where] = ival;
Christian Heimes44720832008-05-26 13:01:01 +00002623
2624 Py_RETURN_NONE;
2625}
2626
2627PyDoc_STRVAR(append__doc__,
2628"B.append(int) -> None\n\
2629\n\
2630Append a single item to the end of B.");
2631static PyObject *
2632bytes_append(PyByteArrayObject *self, PyObject *arg)
2633{
2634 int value;
2635 Py_ssize_t n = Py_SIZE(self);
2636
2637 if (! _getbytevalue(arg, &value))
2638 return NULL;
2639 if (n == PY_SSIZE_T_MAX) {
2640 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson76e96432009-09-06 10:33:12 +00002641 "cannot add more objects to bytearray");
Christian Heimes44720832008-05-26 13:01:01 +00002642 return NULL;
2643 }
2644 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2645 return NULL;
2646
2647 self->ob_bytes[n] = value;
2648
2649 Py_RETURN_NONE;
2650}
2651
2652PyDoc_STRVAR(extend__doc__,
2653"B.extend(iterable int) -> None\n\
2654\n\
2655Append all the elements from the iterator or sequence to the\n\
2656end of B.");
2657static PyObject *
2658bytes_extend(PyByteArrayObject *self, PyObject *arg)
2659{
2660 PyObject *it, *item, *bytes_obj;
2661 Py_ssize_t buf_size = 0, len = 0;
2662 int value;
2663 char *buf;
2664
2665 /* bytes_setslice code only accepts something supporting PEP 3118. */
2666 if (PyObject_CheckBuffer(arg)) {
2667 if (bytes_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1)
2668 return NULL;
2669
2670 Py_RETURN_NONE;
2671 }
2672
2673 it = PyObject_GetIter(arg);
2674 if (it == NULL)
2675 return NULL;
2676
2677 /* Try to determine the length of the argument. 32 is abitrary. */
2678 buf_size = _PyObject_LengthHint(arg, 32);
Georg Brandle9b91212009-04-05 21:26:31 +00002679 if (buf_size == -1) {
2680 Py_DECREF(it);
2681 return NULL;
2682 }
Christian Heimes44720832008-05-26 13:01:01 +00002683
2684 bytes_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
2685 if (bytes_obj == NULL)
2686 return NULL;
2687 buf = PyByteArray_AS_STRING(bytes_obj);
2688
2689 while ((item = PyIter_Next(it)) != NULL) {
2690 if (! _getbytevalue(item, &value)) {
2691 Py_DECREF(item);
2692 Py_DECREF(it);
2693 Py_DECREF(bytes_obj);
2694 return NULL;
2695 }
2696 buf[len++] = value;
2697 Py_DECREF(item);
2698
2699 if (len >= buf_size) {
2700 buf_size = len + (len >> 1) + 1;
2701 if (PyByteArray_Resize((PyObject *)bytes_obj, buf_size) < 0) {
2702 Py_DECREF(it);
2703 Py_DECREF(bytes_obj);
2704 return NULL;
2705 }
2706 /* Recompute the `buf' pointer, since the resizing operation may
2707 have invalidated it. */
2708 buf = PyByteArray_AS_STRING(bytes_obj);
2709 }
2710 }
2711 Py_DECREF(it);
2712
2713 /* Resize down to exact size. */
2714 if (PyByteArray_Resize((PyObject *)bytes_obj, len) < 0) {
2715 Py_DECREF(bytes_obj);
2716 return NULL;
2717 }
2718
2719 if (bytes_setslice(self, Py_SIZE(self), Py_SIZE(self), bytes_obj) == -1)
2720 return NULL;
2721 Py_DECREF(bytes_obj);
2722
2723 Py_RETURN_NONE;
2724}
2725
2726PyDoc_STRVAR(pop__doc__,
2727"B.pop([index]) -> int\n\
2728\n\
2729Remove and return a single item from B. If no index\n\
Andrew M. Kuchlingd8972642008-06-21 13:29:12 +00002730argument is given, will pop the last value.");
Christian Heimes44720832008-05-26 13:01:01 +00002731static PyObject *
2732bytes_pop(PyByteArrayObject *self, PyObject *args)
2733{
2734 int value;
2735 Py_ssize_t where = -1, n = Py_SIZE(self);
2736
2737 if (!PyArg_ParseTuple(args, "|n:pop", &where))
2738 return NULL;
2739
2740 if (n == 0) {
2741 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson76e96432009-09-06 10:33:12 +00002742 "cannot pop an empty bytearray");
Christian Heimes44720832008-05-26 13:01:01 +00002743 return NULL;
2744 }
2745 if (where < 0)
2746 where += Py_SIZE(self);
2747 if (where < 0 || where >= Py_SIZE(self)) {
2748 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2749 return NULL;
2750 }
Antoine Pitrou599db7f2008-12-07 00:07:51 +00002751 if (!_canresize(self))
2752 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00002753
2754 value = self->ob_bytes[where];
2755 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where);
2756 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2757 return NULL;
2758
Mark Dickinsonb61c0352009-09-06 10:05:28 +00002759 return PyInt_FromLong((unsigned char)value);
Christian Heimes44720832008-05-26 13:01:01 +00002760}
2761
2762PyDoc_STRVAR(remove__doc__,
2763"B.remove(int) -> None\n\
2764\n\
2765Remove the first occurance of a value in B.");
2766static PyObject *
2767bytes_remove(PyByteArrayObject *self, PyObject *arg)
2768{
2769 int value;
2770 Py_ssize_t where, n = Py_SIZE(self);
2771
2772 if (! _getbytevalue(arg, &value))
2773 return NULL;
2774
2775 for (where = 0; where < n; where++) {
2776 if (self->ob_bytes[where] == value)
2777 break;
2778 }
2779 if (where == n) {
Mark Dickinson76e96432009-09-06 10:33:12 +00002780 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes44720832008-05-26 13:01:01 +00002781 return NULL;
2782 }
Antoine Pitrou599db7f2008-12-07 00:07:51 +00002783 if (!_canresize(self))
2784 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00002785
2786 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where);
2787 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2788 return NULL;
2789
2790 Py_RETURN_NONE;
2791}
2792
2793/* XXX These two helpers could be optimized if argsize == 1 */
2794
2795static Py_ssize_t
2796lstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
2797 void *argptr, Py_ssize_t argsize)
2798{
2799 Py_ssize_t i = 0;
2800 while (i < mysize && memchr(argptr, myptr[i], argsize))
2801 i++;
2802 return i;
2803}
2804
2805static Py_ssize_t
2806rstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
2807 void *argptr, Py_ssize_t argsize)
2808{
2809 Py_ssize_t i = mysize - 1;
2810 while (i >= 0 && memchr(argptr, myptr[i], argsize))
2811 i--;
2812 return i + 1;
2813}
2814
2815PyDoc_STRVAR(strip__doc__,
2816"B.strip([bytes]) -> bytearray\n\
2817\n\
2818Strip leading and trailing bytes contained in the argument.\n\
2819If the argument is omitted, strip ASCII whitespace.");
2820static PyObject *
2821bytes_strip(PyByteArrayObject *self, PyObject *args)
2822{
2823 Py_ssize_t left, right, mysize, argsize;
2824 void *myptr, *argptr;
2825 PyObject *arg = Py_None;
2826 Py_buffer varg;
2827 if (!PyArg_ParseTuple(args, "|O:strip", &arg))
2828 return NULL;
2829 if (arg == Py_None) {
2830 argptr = "\t\n\r\f\v ";
2831 argsize = 6;
2832 }
2833 else {
2834 if (_getbuffer(arg, &varg) < 0)
2835 return NULL;
2836 argptr = varg.buf;
2837 argsize = varg.len;
2838 }
2839 myptr = self->ob_bytes;
2840 mysize = Py_SIZE(self);
2841 left = lstrip_helper(myptr, mysize, argptr, argsize);
2842 if (left == mysize)
2843 right = left;
2844 else
2845 right = rstrip_helper(myptr, mysize, argptr, argsize);
2846 if (arg != Py_None)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002847 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00002848 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2849}
2850
2851PyDoc_STRVAR(lstrip__doc__,
2852"B.lstrip([bytes]) -> bytearray\n\
2853\n\
2854Strip leading bytes contained in the argument.\n\
2855If the argument is omitted, strip leading ASCII whitespace.");
2856static PyObject *
2857bytes_lstrip(PyByteArrayObject *self, PyObject *args)
2858{
2859 Py_ssize_t left, right, mysize, argsize;
2860 void *myptr, *argptr;
2861 PyObject *arg = Py_None;
2862 Py_buffer varg;
2863 if (!PyArg_ParseTuple(args, "|O:lstrip", &arg))
2864 return NULL;
2865 if (arg == Py_None) {
2866 argptr = "\t\n\r\f\v ";
2867 argsize = 6;
2868 }
2869 else {
2870 if (_getbuffer(arg, &varg) < 0)
2871 return NULL;
2872 argptr = varg.buf;
2873 argsize = varg.len;
2874 }
2875 myptr = self->ob_bytes;
2876 mysize = Py_SIZE(self);
2877 left = lstrip_helper(myptr, mysize, argptr, argsize);
2878 right = mysize;
2879 if (arg != Py_None)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002880 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00002881 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2882}
2883
2884PyDoc_STRVAR(rstrip__doc__,
2885"B.rstrip([bytes]) -> bytearray\n\
2886\n\
2887Strip trailing bytes contained in the argument.\n\
2888If the argument is omitted, strip trailing ASCII whitespace.");
2889static PyObject *
2890bytes_rstrip(PyByteArrayObject *self, PyObject *args)
2891{
2892 Py_ssize_t left, right, mysize, argsize;
2893 void *myptr, *argptr;
2894 PyObject *arg = Py_None;
2895 Py_buffer varg;
2896 if (!PyArg_ParseTuple(args, "|O:rstrip", &arg))
2897 return NULL;
2898 if (arg == Py_None) {
2899 argptr = "\t\n\r\f\v ";
2900 argsize = 6;
2901 }
2902 else {
2903 if (_getbuffer(arg, &varg) < 0)
2904 return NULL;
2905 argptr = varg.buf;
2906 argsize = varg.len;
2907 }
2908 myptr = self->ob_bytes;
2909 mysize = Py_SIZE(self);
2910 left = 0;
2911 right = rstrip_helper(myptr, mysize, argptr, argsize);
2912 if (arg != Py_None)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002913 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00002914 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2915}
2916
2917PyDoc_STRVAR(decode_doc,
2918"B.decode([encoding[, errors]]) -> unicode object.\n\
2919\n\
2920Decodes B using the codec registered for encoding. encoding defaults\n\
2921to the default encoding. errors may be given to set a different error\n\
2922handling scheme. Default is 'strict' meaning that encoding errors raise\n\
2923a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
2924as well as any other name registered with codecs.register_error that is\n\
2925able to handle UnicodeDecodeErrors.");
2926
2927static PyObject *
2928bytes_decode(PyObject *self, PyObject *args)
2929{
2930 const char *encoding = NULL;
2931 const char *errors = NULL;
2932
2933 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
2934 return NULL;
2935 if (encoding == NULL)
2936 encoding = PyUnicode_GetDefaultEncoding();
2937 return PyCodec_Decode(self, encoding, errors);
2938}
2939
2940PyDoc_STRVAR(alloc_doc,
2941"B.__alloc__() -> int\n\
2942\n\
2943Returns the number of bytes actually allocated.");
2944
2945static PyObject *
2946bytes_alloc(PyByteArrayObject *self)
2947{
2948 return PyInt_FromSsize_t(self->ob_alloc);
2949}
2950
2951PyDoc_STRVAR(join_doc,
2952"B.join(iterable_of_bytes) -> bytes\n\
2953\n\
2954Concatenates any number of bytearray objects, with B in between each pair.");
2955
2956static PyObject *
2957bytes_join(PyByteArrayObject *self, PyObject *it)
2958{
2959 PyObject *seq;
2960 Py_ssize_t mysize = Py_SIZE(self);
2961 Py_ssize_t i;
2962 Py_ssize_t n;
2963 PyObject **items;
2964 Py_ssize_t totalsize = 0;
2965 PyObject *result;
2966 char *dest;
2967
2968 seq = PySequence_Fast(it, "can only join an iterable");
2969 if (seq == NULL)
2970 return NULL;
2971 n = PySequence_Fast_GET_SIZE(seq);
2972 items = PySequence_Fast_ITEMS(seq);
2973
2974 /* Compute the total size, and check that they are all bytes */
2975 /* XXX Shouldn't we use _getbuffer() on these items instead? */
2976 for (i = 0; i < n; i++) {
2977 PyObject *obj = items[i];
2978 if (!PyByteArray_Check(obj) && !PyBytes_Check(obj)) {
2979 PyErr_Format(PyExc_TypeError,
2980 "can only join an iterable of bytes "
2981 "(item %ld has type '%.100s')",
2982 /* XXX %ld isn't right on Win64 */
2983 (long)i, Py_TYPE(obj)->tp_name);
2984 goto error;
2985 }
2986 if (i > 0)
2987 totalsize += mysize;
2988 totalsize += Py_SIZE(obj);
2989 if (totalsize < 0) {
2990 PyErr_NoMemory();
2991 goto error;
2992 }
2993 }
2994
2995 /* Allocate the result, and copy the bytes */
2996 result = PyByteArray_FromStringAndSize(NULL, totalsize);
2997 if (result == NULL)
2998 goto error;
2999 dest = PyByteArray_AS_STRING(result);
3000 for (i = 0; i < n; i++) {
3001 PyObject *obj = items[i];
3002 Py_ssize_t size = Py_SIZE(obj);
3003 char *buf;
3004 if (PyByteArray_Check(obj))
3005 buf = PyByteArray_AS_STRING(obj);
3006 else
3007 buf = PyBytes_AS_STRING(obj);
3008 if (i) {
3009 memcpy(dest, self->ob_bytes, mysize);
3010 dest += mysize;
3011 }
3012 memcpy(dest, buf, size);
3013 dest += size;
3014 }
3015
3016 /* Done */
3017 Py_DECREF(seq);
3018 return result;
3019
3020 /* Error handling */
3021 error:
3022 Py_DECREF(seq);
3023 return NULL;
3024}
3025
3026PyDoc_STRVAR(fromhex_doc,
3027"bytearray.fromhex(string) -> bytearray\n\
3028\n\
3029Create a bytearray object from a string of hexadecimal numbers.\n\
3030Spaces between two numbers are accepted.\n\
3031Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
3032
3033static int
3034hex_digit_to_int(Py_UNICODE c)
3035{
3036 if (c >= 128)
3037 return -1;
3038 if (ISDIGIT(c))
3039 return c - '0';
3040 else {
3041 if (ISUPPER(c))
3042 c = TOLOWER(c);
3043 if (c >= 'a' && c <= 'f')
3044 return c - 'a' + 10;
3045 }
3046 return -1;
3047}
3048
3049static PyObject *
3050bytes_fromhex(PyObject *cls, PyObject *args)
3051{
3052 PyObject *newbytes, *hexobj;
3053 char *buf;
3054 Py_UNICODE *hex;
3055 Py_ssize_t hexlen, byteslen, i, j;
3056 int top, bot;
3057
3058 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
3059 return NULL;
3060 assert(PyUnicode_Check(hexobj));
3061 hexlen = PyUnicode_GET_SIZE(hexobj);
3062 hex = PyUnicode_AS_UNICODE(hexobj);
3063 byteslen = hexlen/2; /* This overestimates if there are spaces */
3064 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
3065 if (!newbytes)
3066 return NULL;
3067 buf = PyByteArray_AS_STRING(newbytes);
3068 for (i = j = 0; i < hexlen; i += 2) {
3069 /* skip over spaces in the input */
3070 while (hex[i] == ' ')
3071 i++;
3072 if (i >= hexlen)
3073 break;
3074 top = hex_digit_to_int(hex[i]);
3075 bot = hex_digit_to_int(hex[i+1]);
3076 if (top == -1 || bot == -1) {
3077 PyErr_Format(PyExc_ValueError,
3078 "non-hexadecimal number found in "
3079 "fromhex() arg at position %zd", i);
3080 goto error;
3081 }
3082 buf[j++] = (top << 4) + bot;
3083 }
3084 if (PyByteArray_Resize(newbytes, j) < 0)
3085 goto error;
3086 return newbytes;
3087
3088 error:
3089 Py_DECREF(newbytes);
3090 return NULL;
3091}
3092
3093PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3094
3095static PyObject *
3096bytes_reduce(PyByteArrayObject *self)
3097{
3098 PyObject *latin1, *dict;
3099 if (self->ob_bytes)
3100 latin1 = PyUnicode_DecodeLatin1(self->ob_bytes,
3101 Py_SIZE(self), NULL);
3102 else
3103 latin1 = PyUnicode_FromString("");
3104
3105 dict = PyObject_GetAttrString((PyObject *)self, "__dict__");
3106 if (dict == NULL) {
3107 PyErr_Clear();
3108 dict = Py_None;
3109 Py_INCREF(dict);
3110 }
3111
3112 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
3113}
3114
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003115PyDoc_STRVAR(sizeof_doc,
3116"B.__sizeof__() -> int\n\
3117 \n\
3118Returns the size of B in memory, in bytes");
3119static PyObject *
3120bytes_sizeof(PyByteArrayObject *self)
3121{
Georg Brandle9b91212009-04-05 21:26:31 +00003122 Py_ssize_t res;
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003123
Georg Brandle9b91212009-04-05 21:26:31 +00003124 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
3125 return PyInt_FromSsize_t(res);
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003126}
3127
Christian Heimes44720832008-05-26 13:01:01 +00003128static PySequenceMethods bytes_as_sequence = {
3129 (lenfunc)bytes_length, /* sq_length */
3130 (binaryfunc)PyByteArray_Concat, /* sq_concat */
3131 (ssizeargfunc)bytes_repeat, /* sq_repeat */
3132 (ssizeargfunc)bytes_getitem, /* sq_item */
3133 0, /* sq_slice */
3134 (ssizeobjargproc)bytes_setitem, /* sq_ass_item */
3135 0, /* sq_ass_slice */
3136 (objobjproc)bytes_contains, /* sq_contains */
3137 (binaryfunc)bytes_iconcat, /* sq_inplace_concat */
3138 (ssizeargfunc)bytes_irepeat, /* sq_inplace_repeat */
3139};
3140
3141static PyMappingMethods bytes_as_mapping = {
3142 (lenfunc)bytes_length,
3143 (binaryfunc)bytes_subscript,
3144 (objobjargproc)bytes_ass_subscript,
3145};
3146
3147static PyBufferProcs bytes_as_buffer = {
3148 (readbufferproc)bytes_buffer_getreadbuf,
3149 (writebufferproc)bytes_buffer_getwritebuf,
3150 (segcountproc)bytes_buffer_getsegcount,
3151 (charbufferproc)bytes_buffer_getcharbuf,
3152 (getbufferproc)bytes_getbuffer,
3153 (releasebufferproc)bytes_releasebuffer,
3154};
3155
3156static PyMethodDef
3157bytes_methods[] = {
3158 {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc},
3159 {"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc},
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003160 {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, sizeof_doc},
Christian Heimes44720832008-05-26 13:01:01 +00003161 {"append", (PyCFunction)bytes_append, METH_O, append__doc__},
3162 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
3163 _Py_capitalize__doc__},
3164 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
3165 {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__},
3166 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc},
3167 {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, endswith__doc__},
3168 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS,
3169 expandtabs__doc__},
3170 {"extend", (PyCFunction)bytes_extend, METH_O, extend__doc__},
3171 {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__},
3172 {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS,
3173 fromhex_doc},
3174 {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__},
3175 {"insert", (PyCFunction)bytes_insert, METH_VARARGS, insert__doc__},
3176 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
3177 _Py_isalnum__doc__},
3178 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
3179 _Py_isalpha__doc__},
3180 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
3181 _Py_isdigit__doc__},
3182 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
3183 _Py_islower__doc__},
3184 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
3185 _Py_isspace__doc__},
3186 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
3187 _Py_istitle__doc__},
3188 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
3189 _Py_isupper__doc__},
3190 {"join", (PyCFunction)bytes_join, METH_O, join_doc},
3191 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
3192 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
3193 {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__},
3194 {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__},
3195 {"pop", (PyCFunction)bytes_pop, METH_VARARGS, pop__doc__},
3196 {"remove", (PyCFunction)bytes_remove, METH_O, remove__doc__},
3197 {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__},
3198 {"reverse", (PyCFunction)bytes_reverse, METH_NOARGS, reverse__doc__},
3199 {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__},
3200 {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__},
3201 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
3202 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, rpartition__doc__},
3203 {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__},
3204 {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__},
3205 {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__},
3206 {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS,
3207 splitlines__doc__},
3208 {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS ,
3209 startswith__doc__},
3210 {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__},
3211 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
3212 _Py_swapcase__doc__},
3213 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
3214 {"translate", (PyCFunction)bytes_translate, METH_VARARGS,
3215 translate__doc__},
3216 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
3217 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
3218 {NULL}
3219};
3220
3221PyDoc_STRVAR(bytes_doc,
3222"bytearray(iterable_of_ints) -> bytearray.\n\
3223bytearray(string, encoding[, errors]) -> bytearray.\n\
3224bytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray.\n\
3225bytearray(memory_view) -> bytearray.\n\
3226\n\
3227Construct an mutable bytearray object from:\n\
3228 - an iterable yielding integers in range(256)\n\
3229 - a text string encoded using the specified encoding\n\
3230 - a bytes or a bytearray object\n\
3231 - any object implementing the buffer API.\n\
3232\n\
3233bytearray(int) -> bytearray.\n\
3234\n\
3235Construct a zero-initialized bytearray of the given length.");
3236
3237
3238static PyObject *bytes_iter(PyObject *seq);
3239
3240PyTypeObject PyByteArray_Type = {
3241 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3242 "bytearray",
3243 sizeof(PyByteArrayObject),
3244 0,
3245 (destructor)bytes_dealloc, /* tp_dealloc */
3246 0, /* tp_print */
3247 0, /* tp_getattr */
3248 0, /* tp_setattr */
3249 0, /* tp_compare */
3250 (reprfunc)bytes_repr, /* tp_repr */
3251 0, /* tp_as_number */
3252 &bytes_as_sequence, /* tp_as_sequence */
3253 &bytes_as_mapping, /* tp_as_mapping */
3254 0, /* tp_hash */
3255 0, /* tp_call */
3256 bytes_str, /* tp_str */
3257 PyObject_GenericGetAttr, /* tp_getattro */
3258 0, /* tp_setattro */
3259 &bytes_as_buffer, /* tp_as_buffer */
3260 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
3261 Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */
3262 bytes_doc, /* tp_doc */
3263 0, /* tp_traverse */
3264 0, /* tp_clear */
3265 (richcmpfunc)bytes_richcompare, /* tp_richcompare */
3266 0, /* tp_weaklistoffset */
3267 bytes_iter, /* tp_iter */
3268 0, /* tp_iternext */
3269 bytes_methods, /* tp_methods */
3270 0, /* tp_members */
3271 0, /* tp_getset */
3272 0, /* tp_base */
3273 0, /* tp_dict */
3274 0, /* tp_descr_get */
3275 0, /* tp_descr_set */
3276 0, /* tp_dictoffset */
3277 (initproc)bytes_init, /* tp_init */
3278 PyType_GenericAlloc, /* tp_alloc */
3279 PyType_GenericNew, /* tp_new */
3280 PyObject_Del, /* tp_free */
3281};
3282
3283/*********************** Bytes Iterator ****************************/
3284
3285typedef struct {
3286 PyObject_HEAD
3287 Py_ssize_t it_index;
3288 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
3289} bytesiterobject;
3290
3291static void
3292bytesiter_dealloc(bytesiterobject *it)
3293{
3294 _PyObject_GC_UNTRACK(it);
3295 Py_XDECREF(it->it_seq);
3296 PyObject_GC_Del(it);
3297}
3298
3299static int
3300bytesiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
3301{
3302 Py_VISIT(it->it_seq);
3303 return 0;
3304}
3305
3306static PyObject *
3307bytesiter_next(bytesiterobject *it)
3308{
3309 PyByteArrayObject *seq;
3310 PyObject *item;
3311
3312 assert(it != NULL);
3313 seq = it->it_seq;
3314 if (seq == NULL)
3315 return NULL;
3316 assert(PyByteArray_Check(seq));
3317
3318 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
3319 item = PyInt_FromLong(
3320 (unsigned char)seq->ob_bytes[it->it_index]);
3321 if (item != NULL)
3322 ++it->it_index;
3323 return item;
3324 }
3325
3326 Py_DECREF(seq);
3327 it->it_seq = NULL;
3328 return NULL;
3329}
3330
3331static PyObject *
3332bytesiter_length_hint(bytesiterobject *it)
3333{
3334 Py_ssize_t len = 0;
3335 if (it->it_seq)
3336 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
3337 return PyInt_FromSsize_t(len);
3338}
3339
3340PyDoc_STRVAR(length_hint_doc,
3341 "Private method returning an estimate of len(list(it)).");
3342
3343static PyMethodDef bytesiter_methods[] = {
3344 {"__length_hint__", (PyCFunction)bytesiter_length_hint, METH_NOARGS,
3345 length_hint_doc},
3346 {NULL, NULL} /* sentinel */
3347};
3348
3349PyTypeObject PyByteArrayIter_Type = {
3350 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3351 "bytearray_iterator", /* tp_name */
3352 sizeof(bytesiterobject), /* tp_basicsize */
3353 0, /* tp_itemsize */
3354 /* methods */
3355 (destructor)bytesiter_dealloc, /* tp_dealloc */
3356 0, /* tp_print */
3357 0, /* tp_getattr */
3358 0, /* tp_setattr */
3359 0, /* tp_compare */
3360 0, /* tp_repr */
3361 0, /* tp_as_number */
3362 0, /* tp_as_sequence */
3363 0, /* tp_as_mapping */
3364 0, /* tp_hash */
3365 0, /* tp_call */
3366 0, /* tp_str */
3367 PyObject_GenericGetAttr, /* tp_getattro */
3368 0, /* tp_setattro */
3369 0, /* tp_as_buffer */
3370 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3371 0, /* tp_doc */
3372 (traverseproc)bytesiter_traverse, /* tp_traverse */
3373 0, /* tp_clear */
3374 0, /* tp_richcompare */
3375 0, /* tp_weaklistoffset */
3376 PyObject_SelfIter, /* tp_iter */
3377 (iternextfunc)bytesiter_next, /* tp_iternext */
3378 bytesiter_methods, /* tp_methods */
3379 0,
3380};
3381
3382static PyObject *
3383bytes_iter(PyObject *seq)
3384{
3385 bytesiterobject *it;
3386
3387 if (!PyByteArray_Check(seq)) {
3388 PyErr_BadInternalCall();
3389 return NULL;
3390 }
3391 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3392 if (it == NULL)
3393 return NULL;
3394 it->it_index = 0;
3395 Py_INCREF(seq);
3396 it->it_seq = (PyByteArrayObject *)seq;
3397 _PyObject_GC_TRACK(it);
3398 return (PyObject *)it;
3399}