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