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