blob: 56071a0cce18317eac712f617eda6ce03cc5e776 [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
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000157static int
158_canresize(PyByteArrayObject *self)
159{
160 if (self->ob_exports > 0) {
161 PyErr_SetString(PyExc_BufferError,
162 "Existing exports of data: object cannot be re-sized");
163 return 0;
164 }
165 return 1;
166}
167
Christian Heimes44720832008-05-26 13:01:01 +0000168/* Direct API functions */
169
170PyObject *
171PyByteArray_FromObject(PyObject *input)
172{
173 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
174 input, NULL);
175}
176
177PyObject *
178PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
179{
180 PyByteArrayObject *new;
181 Py_ssize_t alloc;
182
183 if (size < 0) {
184 PyErr_SetString(PyExc_SystemError,
185 "Negative size passed to PyByteArray_FromStringAndSize");
186 return NULL;
187 }
188
189 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
190 if (new == NULL)
191 return NULL;
192
193 if (size == 0) {
194 new->ob_bytes = NULL;
195 alloc = 0;
196 }
197 else {
198 alloc = size + 1;
199 new->ob_bytes = PyMem_Malloc(alloc);
200 if (new->ob_bytes == NULL) {
201 Py_DECREF(new);
202 return PyErr_NoMemory();
203 }
204 if (bytes != NULL)
205 memcpy(new->ob_bytes, bytes, size);
206 new->ob_bytes[size] = '\0'; /* Trailing null byte */
207 }
208 Py_SIZE(new) = size;
209 new->ob_alloc = alloc;
210 new->ob_exports = 0;
211
212 return (PyObject *)new;
213}
214
215Py_ssize_t
216PyByteArray_Size(PyObject *self)
217{
218 assert(self != NULL);
219 assert(PyByteArray_Check(self));
220
221 return PyByteArray_GET_SIZE(self);
222}
223
224char *
225PyByteArray_AsString(PyObject *self)
226{
227 assert(self != NULL);
228 assert(PyByteArray_Check(self));
229
230 return PyByteArray_AS_STRING(self);
231}
232
233int
234PyByteArray_Resize(PyObject *self, Py_ssize_t size)
235{
236 void *sval;
237 Py_ssize_t alloc = ((PyByteArrayObject *)self)->ob_alloc;
238
239 assert(self != NULL);
240 assert(PyByteArray_Check(self));
241 assert(size >= 0);
242
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000243 if (size == Py_SIZE(self)) {
244 return 0;
245 }
246 if (!_canresize((PyByteArrayObject *)self)) {
247 return -1;
248 }
249
Christian Heimes44720832008-05-26 13:01:01 +0000250 if (size < alloc / 2) {
251 /* Major downsize; resize down to exact size */
252 alloc = size + 1;
253 }
254 else if (size < alloc) {
255 /* Within allocated size; quick exit */
256 Py_SIZE(self) = size;
257 ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null */
258 return 0;
259 }
260 else if (size <= alloc * 1.125) {
261 /* Moderate upsize; overallocate similar to list_resize() */
262 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
263 }
264 else {
265 /* Major upsize; resize up to exact size */
266 alloc = size + 1;
267 }
268
Christian Heimes44720832008-05-26 13:01:01 +0000269 sval = PyMem_Realloc(((PyByteArrayObject *)self)->ob_bytes, alloc);
270 if (sval == NULL) {
271 PyErr_NoMemory();
272 return -1;
273 }
274
275 ((PyByteArrayObject *)self)->ob_bytes = sval;
276 Py_SIZE(self) = size;
277 ((PyByteArrayObject *)self)->ob_alloc = alloc;
278 ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */
279
280 return 0;
281}
282
283PyObject *
284PyByteArray_Concat(PyObject *a, PyObject *b)
285{
286 Py_ssize_t size;
287 Py_buffer va, vb;
288 PyByteArrayObject *result = NULL;
289
290 va.len = -1;
291 vb.len = -1;
292 if (_getbuffer(a, &va) < 0 ||
293 _getbuffer(b, &vb) < 0) {
294 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
295 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
296 goto done;
297 }
298
299 size = va.len + vb.len;
300 if (size < 0) {
301 return PyErr_NoMemory();
302 goto done;
303 }
304
305 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
306 if (result != NULL) {
307 memcpy(result->ob_bytes, va.buf, va.len);
308 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
309 }
310
311 done:
312 if (va.len != -1)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000313 PyBuffer_Release(&va);
Christian Heimes44720832008-05-26 13:01:01 +0000314 if (vb.len != -1)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000315 PyBuffer_Release(&vb);
Christian Heimes44720832008-05-26 13:01:01 +0000316 return (PyObject *)result;
317}
318
319/* Functions stuffed into the type object */
320
321static Py_ssize_t
322bytes_length(PyByteArrayObject *self)
323{
324 return Py_SIZE(self);
325}
326
327static PyObject *
328bytes_iconcat(PyByteArrayObject *self, PyObject *other)
329{
330 Py_ssize_t mysize;
331 Py_ssize_t size;
332 Py_buffer vo;
333
334 if (_getbuffer(other, &vo) < 0) {
335 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
336 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
337 return NULL;
338 }
339
340 mysize = Py_SIZE(self);
341 size = mysize + vo.len;
342 if (size < 0) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000343 PyBuffer_Release(&vo);
Christian Heimes44720832008-05-26 13:01:01 +0000344 return PyErr_NoMemory();
345 }
346 if (size < self->ob_alloc) {
347 Py_SIZE(self) = size;
348 self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */
349 }
350 else if (PyByteArray_Resize((PyObject *)self, size) < 0) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000351 PyBuffer_Release(&vo);
Christian Heimes44720832008-05-26 13:01:01 +0000352 return NULL;
353 }
354 memcpy(self->ob_bytes + mysize, vo.buf, vo.len);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000355 PyBuffer_Release(&vo);
Christian Heimes44720832008-05-26 13:01:01 +0000356 Py_INCREF(self);
357 return (PyObject *)self;
358}
359
360static PyObject *
361bytes_repeat(PyByteArrayObject *self, Py_ssize_t count)
362{
363 PyByteArrayObject *result;
364 Py_ssize_t mysize;
365 Py_ssize_t size;
366
367 if (count < 0)
368 count = 0;
369 mysize = Py_SIZE(self);
370 size = mysize * count;
371 if (count != 0 && size / count != mysize)
372 return PyErr_NoMemory();
373 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
374 if (result != NULL && size != 0) {
375 if (mysize == 1)
376 memset(result->ob_bytes, self->ob_bytes[0], size);
377 else {
378 Py_ssize_t i;
379 for (i = 0; i < count; i++)
380 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
381 }
382 }
383 return (PyObject *)result;
384}
385
386static PyObject *
387bytes_irepeat(PyByteArrayObject *self, Py_ssize_t count)
388{
389 Py_ssize_t mysize;
390 Py_ssize_t size;
391
392 if (count < 0)
393 count = 0;
394 mysize = Py_SIZE(self);
395 size = mysize * count;
396 if (count != 0 && size / count != mysize)
397 return PyErr_NoMemory();
398 if (size < self->ob_alloc) {
399 Py_SIZE(self) = size;
400 self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */
401 }
402 else if (PyByteArray_Resize((PyObject *)self, size) < 0)
403 return NULL;
404
405 if (mysize == 1)
406 memset(self->ob_bytes, self->ob_bytes[0], size);
407 else {
408 Py_ssize_t i;
409 for (i = 1; i < count; i++)
410 memcpy(self->ob_bytes + i*mysize, self->ob_bytes, mysize);
411 }
412
413 Py_INCREF(self);
414 return (PyObject *)self;
415}
416
417static PyObject *
418bytes_getitem(PyByteArrayObject *self, Py_ssize_t i)
419{
420 if (i < 0)
421 i += Py_SIZE(self);
422 if (i < 0 || i >= Py_SIZE(self)) {
423 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
424 return NULL;
425 }
426 return PyInt_FromLong((unsigned char)(self->ob_bytes[i]));
427}
428
429static PyObject *
Georg Brandl3e483f62008-07-16 22:57:41 +0000430bytes_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes44720832008-05-26 13:01:01 +0000431{
Georg Brandl3e483f62008-07-16 22:57:41 +0000432 if (PyIndex_Check(index)) {
433 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes44720832008-05-26 13:01:01 +0000434
435 if (i == -1 && PyErr_Occurred())
436 return NULL;
437
438 if (i < 0)
439 i += PyByteArray_GET_SIZE(self);
440
441 if (i < 0 || i >= Py_SIZE(self)) {
442 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
443 return NULL;
444 }
445 return PyInt_FromLong((unsigned char)(self->ob_bytes[i]));
446 }
Georg Brandl3e483f62008-07-16 22:57:41 +0000447 else if (PySlice_Check(index)) {
Christian Heimes44720832008-05-26 13:01:01 +0000448 Py_ssize_t start, stop, step, slicelength, cur, i;
Georg Brandl3e483f62008-07-16 22:57:41 +0000449 if (PySlice_GetIndicesEx((PySliceObject *)index,
Christian Heimes44720832008-05-26 13:01:01 +0000450 PyByteArray_GET_SIZE(self),
451 &start, &stop, &step, &slicelength) < 0) {
452 return NULL;
453 }
454
455 if (slicelength <= 0)
456 return PyByteArray_FromStringAndSize("", 0);
457 else if (step == 1) {
458 return PyByteArray_FromStringAndSize(self->ob_bytes + start,
459 slicelength);
460 }
461 else {
462 char *source_buf = PyByteArray_AS_STRING(self);
463 char *result_buf = (char *)PyMem_Malloc(slicelength);
464 PyObject *result;
465
466 if (result_buf == NULL)
467 return PyErr_NoMemory();
468
469 for (cur = start, i = 0; i < slicelength;
470 cur += step, i++) {
471 result_buf[i] = source_buf[cur];
472 }
473 result = PyByteArray_FromStringAndSize(result_buf, slicelength);
474 PyMem_Free(result_buf);
475 return result;
476 }
477 }
478 else {
479 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers");
480 return NULL;
481 }
482}
483
484static int
485bytes_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
486 PyObject *values)
487{
488 Py_ssize_t avail, needed;
489 void *bytes;
490 Py_buffer vbytes;
491 int res = 0;
492
493 vbytes.len = -1;
494 if (values == (PyObject *)self) {
495 /* Make a copy and call this function recursively */
496 int err;
497 values = PyByteArray_FromObject(values);
498 if (values == NULL)
499 return -1;
500 err = bytes_setslice(self, lo, hi, values);
501 Py_DECREF(values);
502 return err;
503 }
504 if (values == NULL) {
505 /* del b[lo:hi] */
506 bytes = NULL;
507 needed = 0;
508 }
509 else {
510 if (_getbuffer(values, &vbytes) < 0) {
511 PyErr_Format(PyExc_TypeError,
Neal Norwitzc86b54c2008-07-20 19:35:23 +0000512 "can't set bytearray slice from %.100s",
Christian Heimes44720832008-05-26 13:01:01 +0000513 Py_TYPE(values)->tp_name);
514 return -1;
515 }
516 needed = vbytes.len;
517 bytes = vbytes.buf;
518 }
519
520 if (lo < 0)
521 lo = 0;
522 if (hi < lo)
523 hi = lo;
524 if (hi > Py_SIZE(self))
525 hi = Py_SIZE(self);
526
527 avail = hi - lo;
528 if (avail < 0)
529 lo = hi = avail = 0;
530
531 if (avail != needed) {
532 if (avail > needed) {
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000533 if (!_canresize(self)) {
534 res = -1;
535 goto finish;
536 }
Christian Heimes44720832008-05-26 13:01:01 +0000537 /*
538 0 lo hi old_size
539 | |<----avail----->|<-----tomove------>|
540 | |<-needed->|<-----tomove------>|
541 0 lo new_hi new_size
542 */
543 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
544 Py_SIZE(self) - hi);
545 }
546 /* XXX(nnorwitz): need to verify this can't overflow! */
547 if (PyByteArray_Resize((PyObject *)self,
548 Py_SIZE(self) + needed - avail) < 0) {
549 res = -1;
550 goto finish;
551 }
552 if (avail < needed) {
553 /*
554 0 lo hi old_size
555 | |<-avail->|<-----tomove------>|
556 | |<----needed---->|<-----tomove------>|
557 0 lo new_hi new_size
558 */
559 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
560 Py_SIZE(self) - lo - needed);
561 }
562 }
563
564 if (needed > 0)
565 memcpy(self->ob_bytes + lo, bytes, needed);
566
567
568 finish:
569 if (vbytes.len != -1)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000570 PyBuffer_Release(&vbytes);
Christian Heimes44720832008-05-26 13:01:01 +0000571 return res;
572}
573
574static int
575bytes_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
576{
577 int ival;
578
579 if (i < 0)
580 i += Py_SIZE(self);
581
582 if (i < 0 || i >= Py_SIZE(self)) {
583 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
584 return -1;
585 }
586
587 if (value == NULL)
588 return bytes_setslice(self, i, i+1, NULL);
589
590 if (!_getbytevalue(value, &ival))
591 return -1;
592
593 self->ob_bytes[i] = ival;
594 return 0;
595}
596
597static int
Georg Brandl3e483f62008-07-16 22:57:41 +0000598bytes_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes44720832008-05-26 13:01:01 +0000599{
600 Py_ssize_t start, stop, step, slicelen, needed;
601 char *bytes;
602
Georg Brandl3e483f62008-07-16 22:57:41 +0000603 if (PyIndex_Check(index)) {
604 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes44720832008-05-26 13:01:01 +0000605
606 if (i == -1 && PyErr_Occurred())
607 return -1;
608
609 if (i < 0)
610 i += PyByteArray_GET_SIZE(self);
611
612 if (i < 0 || i >= Py_SIZE(self)) {
613 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
614 return -1;
615 }
616
617 if (values == NULL) {
618 /* Fall through to slice assignment */
619 start = i;
620 stop = i + 1;
621 step = 1;
622 slicelen = 1;
623 }
624 else {
Georg Brandl3e483f62008-07-16 22:57:41 +0000625 int ival;
626 if (!_getbytevalue(values, &ival))
Christian Heimes44720832008-05-26 13:01:01 +0000627 return -1;
Christian Heimes44720832008-05-26 13:01:01 +0000628 self->ob_bytes[i] = (char)ival;
629 return 0;
630 }
631 }
Georg Brandl3e483f62008-07-16 22:57:41 +0000632 else if (PySlice_Check(index)) {
633 if (PySlice_GetIndicesEx((PySliceObject *)index,
Christian Heimes44720832008-05-26 13:01:01 +0000634 PyByteArray_GET_SIZE(self),
635 &start, &stop, &step, &slicelen) < 0) {
636 return -1;
637 }
638 }
639 else {
640 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer");
641 return -1;
642 }
643
644 if (values == NULL) {
645 bytes = NULL;
646 needed = 0;
647 }
648 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
649 /* Make a copy an call this function recursively */
650 int err;
651 values = PyByteArray_FromObject(values);
652 if (values == NULL)
653 return -1;
Georg Brandl3e483f62008-07-16 22:57:41 +0000654 err = bytes_ass_subscript(self, index, values);
Christian Heimes44720832008-05-26 13:01:01 +0000655 Py_DECREF(values);
656 return err;
657 }
658 else {
659 assert(PyByteArray_Check(values));
660 bytes = ((PyByteArrayObject *)values)->ob_bytes;
661 needed = Py_SIZE(values);
662 }
663 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
664 if ((step < 0 && start < stop) ||
665 (step > 0 && start > stop))
666 stop = start;
667 if (step == 1) {
668 if (slicelen != needed) {
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000669 if (!_canresize(self))
670 return -1;
Christian Heimes44720832008-05-26 13:01:01 +0000671 if (slicelen > needed) {
672 /*
673 0 start stop old_size
674 | |<---slicelen--->|<-----tomove------>|
675 | |<-needed->|<-----tomove------>|
676 0 lo new_hi new_size
677 */
678 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,
679 Py_SIZE(self) - stop);
680 }
681 if (PyByteArray_Resize((PyObject *)self,
682 Py_SIZE(self) + needed - slicelen) < 0)
683 return -1;
684 if (slicelen < needed) {
685 /*
686 0 lo hi old_size
687 | |<-avail->|<-----tomove------>|
688 | |<----needed---->|<-----tomove------>|
689 0 lo new_hi new_size
690 */
691 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,
692 Py_SIZE(self) - start - needed);
693 }
694 }
695
696 if (needed > 0)
697 memcpy(self->ob_bytes + start, bytes, needed);
698
699 return 0;
700 }
701 else {
702 if (needed == 0) {
703 /* Delete slice */
704 Py_ssize_t cur, i;
705
Antoine Pitrou599db7f2008-12-07 00:07:51 +0000706 if (!_canresize(self))
707 return -1;
Christian Heimes44720832008-05-26 13:01:01 +0000708 if (step < 0) {
709 stop = start + 1;
710 start = stop + step * (slicelen - 1) - 1;
711 step = -step;
712 }
713 for (cur = start, i = 0;
714 i < slicelen; cur += step, i++) {
715 Py_ssize_t lim = step - 1;
716
717 if (cur + step >= PyByteArray_GET_SIZE(self))
718 lim = PyByteArray_GET_SIZE(self) - cur - 1;
719
720 memmove(self->ob_bytes + cur - i,
721 self->ob_bytes + cur + 1, lim);
722 }
723 /* Move the tail of the bytes, in one chunk */
724 cur = start + slicelen*step;
725 if (cur < PyByteArray_GET_SIZE(self)) {
726 memmove(self->ob_bytes + cur - slicelen,
727 self->ob_bytes + cur,
728 PyByteArray_GET_SIZE(self) - cur);
729 }
730 if (PyByteArray_Resize((PyObject *)self,
731 PyByteArray_GET_SIZE(self) - slicelen) < 0)
732 return -1;
733
734 return 0;
735 }
736 else {
737 /* Assign slice */
738 Py_ssize_t cur, i;
739
740 if (needed != slicelen) {
741 PyErr_Format(PyExc_ValueError,
742 "attempt to assign bytes of size %zd "
743 "to extended slice of size %zd",
744 needed, slicelen);
745 return -1;
746 }
747 for (cur = start, i = 0; i < slicelen; cur += step, i++)
748 self->ob_bytes[cur] = bytes[i];
749 return 0;
750 }
751 }
752}
753
754static int
755bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
756{
757 static char *kwlist[] = {"source", "encoding", "errors", 0};
758 PyObject *arg = NULL;
759 const char *encoding = NULL;
760 const char *errors = NULL;
761 Py_ssize_t count;
762 PyObject *it;
763 PyObject *(*iternext)(PyObject *);
764
765 if (Py_SIZE(self) != 0) {
766 /* Empty previous contents (yes, do this first of all!) */
767 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
768 return -1;
769 }
770
771 /* Parse arguments */
Neal Norwitzc86b54c2008-07-20 19:35:23 +0000772 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes44720832008-05-26 13:01:01 +0000773 &arg, &encoding, &errors))
774 return -1;
775
776 /* Make a quick exit if no first argument */
777 if (arg == NULL) {
778 if (encoding != NULL || errors != NULL) {
779 PyErr_SetString(PyExc_TypeError,
780 "encoding or errors without sequence argument");
781 return -1;
782 }
783 return 0;
784 }
785
786 if (PyBytes_Check(arg)) {
787 PyObject *new, *encoded;
788 if (encoding != NULL) {
789 encoded = PyCodec_Encode(arg, encoding, errors);
790 if (encoded == NULL)
791 return -1;
792 assert(PyBytes_Check(encoded));
793 }
794 else {
795 encoded = arg;
796 Py_INCREF(arg);
797 }
798 new = bytes_iconcat(self, arg);
799 Py_DECREF(encoded);
800 if (new == NULL)
801 return -1;
802 Py_DECREF(new);
803 return 0;
804 }
805
806 if (PyUnicode_Check(arg)) {
807 /* Encode via the codec registry */
808 PyObject *encoded, *new;
809 if (encoding == NULL) {
810 PyErr_SetString(PyExc_TypeError,
811 "unicode argument without an encoding");
812 return -1;
813 }
814 encoded = PyCodec_Encode(arg, encoding, errors);
815 if (encoded == NULL)
816 return -1;
817 assert(PyBytes_Check(encoded));
818 new = bytes_iconcat(self, encoded);
819 Py_DECREF(encoded);
820 if (new == NULL)
821 return -1;
822 Py_DECREF(new);
823 return 0;
824 }
825
826 /* If it's not unicode, there can't be encoding or errors */
827 if (encoding != NULL || errors != NULL) {
828 PyErr_SetString(PyExc_TypeError,
829 "encoding or errors without a string argument");
830 return -1;
831 }
832
833 /* Is it an int? */
834 count = PyNumber_AsSsize_t(arg, PyExc_ValueError);
835 if (count == -1 && PyErr_Occurred())
836 PyErr_Clear();
837 else {
838 if (count < 0) {
839 PyErr_SetString(PyExc_ValueError, "negative count");
840 return -1;
841 }
842 if (count > 0) {
843 if (PyByteArray_Resize((PyObject *)self, count))
844 return -1;
845 memset(self->ob_bytes, 0, count);
846 }
847 return 0;
848 }
849
850 /* Use the buffer API */
851 if (PyObject_CheckBuffer(arg)) {
852 Py_ssize_t size;
853 Py_buffer view;
854 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
855 return -1;
856 size = view.len;
857 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
858 if (PyBuffer_ToContiguous(self->ob_bytes, &view, size, 'C') < 0)
859 goto fail;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000860 PyBuffer_Release(&view);
Christian Heimes44720832008-05-26 13:01:01 +0000861 return 0;
862 fail:
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000863 PyBuffer_Release(&view);
Christian Heimes44720832008-05-26 13:01:01 +0000864 return -1;
865 }
866
867 /* XXX Optimize this if the arguments is a list, tuple */
868
869 /* Get the iterator */
870 it = PyObject_GetIter(arg);
871 if (it == NULL)
872 return -1;
873 iternext = *Py_TYPE(it)->tp_iternext;
874
875 /* Run the iterator to exhaustion */
876 for (;;) {
877 PyObject *item;
Georg Brandl3e758462008-07-16 23:10:05 +0000878 int rc, value;
Christian Heimes44720832008-05-26 13:01:01 +0000879
880 /* Get the next item */
881 item = iternext(it);
882 if (item == NULL) {
883 if (PyErr_Occurred()) {
884 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
885 goto error;
886 PyErr_Clear();
887 }
888 break;
889 }
890
891 /* Interpret it as an int (__index__) */
Georg Brandl3e758462008-07-16 23:10:05 +0000892 rc = _getbytevalue(item, &value);
Christian Heimes44720832008-05-26 13:01:01 +0000893 Py_DECREF(item);
Georg Brandl3e758462008-07-16 23:10:05 +0000894 if (!rc)
Christian Heimes44720832008-05-26 13:01:01 +0000895 goto error;
896
Christian Heimes44720832008-05-26 13:01:01 +0000897 /* Append the byte */
898 if (Py_SIZE(self) < self->ob_alloc)
899 Py_SIZE(self)++;
900 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
901 goto error;
902 self->ob_bytes[Py_SIZE(self)-1] = value;
903 }
904
905 /* Clean up and return success */
906 Py_DECREF(it);
907 return 0;
908
909 error:
910 /* Error handling when it != NULL */
911 Py_DECREF(it);
912 return -1;
913}
914
915/* Mostly copied from string_repr, but without the
916 "smart quote" functionality. */
917static PyObject *
918bytes_repr(PyByteArrayObject *self)
919{
920 static const char *hexdigits = "0123456789abcdef";
921 const char *quote_prefix = "bytearray(b";
922 const char *quote_postfix = ")";
923 Py_ssize_t length = Py_SIZE(self);
924 /* 14 == strlen(quote_prefix) + 2 + strlen(quote_postfix) */
925 size_t newsize = 14 + 4 * length;
926 PyObject *v;
927 if (newsize > PY_SSIZE_T_MAX || newsize / 4 - 3 != length) {
928 PyErr_SetString(PyExc_OverflowError,
929 "bytearray object is too large to make repr");
930 return NULL;
931 }
932 v = PyUnicode_FromUnicode(NULL, newsize);
933 if (v == NULL) {
934 return NULL;
935 }
936 else {
937 register Py_ssize_t i;
938 register Py_UNICODE c;
939 register Py_UNICODE *p;
940 int quote;
941
942 /* Figure out which quote to use; single is preferred */
943 quote = '\'';
944 {
945 char *test, *start;
946 start = PyByteArray_AS_STRING(self);
947 for (test = start; test < start+length; ++test) {
948 if (*test == '"') {
949 quote = '\''; /* back to single */
950 goto decided;
951 }
952 else if (*test == '\'')
953 quote = '"';
954 }
955 decided:
956 ;
957 }
958
959 p = PyUnicode_AS_UNICODE(v);
960 while (*quote_prefix)
961 *p++ = *quote_prefix++;
962 *p++ = quote;
963
964 for (i = 0; i < length; i++) {
965 /* There's at least enough room for a hex escape
966 and a closing quote. */
967 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
968 c = self->ob_bytes[i];
969 if (c == '\'' || c == '\\')
970 *p++ = '\\', *p++ = c;
971 else if (c == '\t')
972 *p++ = '\\', *p++ = 't';
973 else if (c == '\n')
974 *p++ = '\\', *p++ = 'n';
975 else if (c == '\r')
976 *p++ = '\\', *p++ = 'r';
977 else if (c == 0)
978 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
979 else if (c < ' ' || c >= 0x7f) {
980 *p++ = '\\';
981 *p++ = 'x';
982 *p++ = hexdigits[(c & 0xf0) >> 4];
983 *p++ = hexdigits[c & 0xf];
984 }
985 else
986 *p++ = c;
987 }
988 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
989 *p++ = quote;
990 while (*quote_postfix) {
991 *p++ = *quote_postfix++;
992 }
993 *p = '\0';
994 if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
995 Py_DECREF(v);
996 return NULL;
997 }
998 return v;
999 }
1000}
1001
1002static PyObject *
1003bytes_str(PyObject *op)
1004{
1005#if 0
1006 if (Py_BytesWarningFlag) {
1007 if (PyErr_WarnEx(PyExc_BytesWarning,
1008 "str() on a bytearray instance", 1))
1009 return NULL;
1010 }
1011 return bytes_repr((PyByteArrayObject*)op);
1012#endif
1013 return PyBytes_FromStringAndSize(((PyByteArrayObject*)op)->ob_bytes, Py_SIZE(op));
1014}
1015
1016static PyObject *
1017bytes_richcompare(PyObject *self, PyObject *other, int op)
1018{
1019 Py_ssize_t self_size, other_size;
1020 Py_buffer self_bytes, other_bytes;
1021 PyObject *res;
1022 Py_ssize_t minsize;
1023 int cmp;
1024
1025 /* Bytes can be compared to anything that supports the (binary)
1026 buffer API. Except that a comparison with Unicode is always an
1027 error, even if the comparison is for equality. */
1028 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
1029 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
1030 if (Py_BytesWarningFlag && op == Py_EQ) {
1031 if (PyErr_WarnEx(PyExc_BytesWarning,
1032 "Comparsion between bytearray and string", 1))
1033 return NULL;
1034 }
1035
1036 Py_INCREF(Py_NotImplemented);
1037 return Py_NotImplemented;
1038 }
1039
1040 self_size = _getbuffer(self, &self_bytes);
1041 if (self_size < 0) {
1042 PyErr_Clear();
1043 Py_INCREF(Py_NotImplemented);
1044 return Py_NotImplemented;
1045 }
1046
1047 other_size = _getbuffer(other, &other_bytes);
1048 if (other_size < 0) {
1049 PyErr_Clear();
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001050 PyBuffer_Release(&self_bytes);
Christian Heimes44720832008-05-26 13:01:01 +00001051 Py_INCREF(Py_NotImplemented);
1052 return Py_NotImplemented;
1053 }
1054
1055 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1056 /* Shortcut: if the lengths differ, the objects differ */
1057 cmp = (op == Py_NE);
1058 }
1059 else {
1060 minsize = self_size;
1061 if (other_size < minsize)
1062 minsize = other_size;
1063
1064 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1065 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1066
1067 if (cmp == 0) {
1068 if (self_size < other_size)
1069 cmp = -1;
1070 else if (self_size > other_size)
1071 cmp = 1;
1072 }
1073
1074 switch (op) {
1075 case Py_LT: cmp = cmp < 0; break;
1076 case Py_LE: cmp = cmp <= 0; break;
1077 case Py_EQ: cmp = cmp == 0; break;
1078 case Py_NE: cmp = cmp != 0; break;
1079 case Py_GT: cmp = cmp > 0; break;
1080 case Py_GE: cmp = cmp >= 0; break;
1081 }
1082 }
1083
1084 res = cmp ? Py_True : Py_False;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001085 PyBuffer_Release(&self_bytes);
1086 PyBuffer_Release(&other_bytes);
Christian Heimes44720832008-05-26 13:01:01 +00001087 Py_INCREF(res);
1088 return res;
1089}
1090
1091static void
1092bytes_dealloc(PyByteArrayObject *self)
1093{
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001094 if (self->ob_exports > 0) {
1095 PyErr_SetString(PyExc_SystemError,
1096 "deallocated bytearray object has exported buffers");
1097 PyErr_Print();
1098 }
Christian Heimes44720832008-05-26 13:01:01 +00001099 if (self->ob_bytes != 0) {
1100 PyMem_Free(self->ob_bytes);
1101 }
1102 Py_TYPE(self)->tp_free((PyObject *)self);
1103}
1104
1105
1106/* -------------------------------------------------------------------- */
1107/* Methods */
1108
1109#define STRINGLIB_CHAR char
1110#define STRINGLIB_CMP memcmp
1111#define STRINGLIB_LEN PyByteArray_GET_SIZE
1112#define STRINGLIB_STR PyByteArray_AS_STRING
1113#define STRINGLIB_NEW PyByteArray_FromStringAndSize
1114#define STRINGLIB_EMPTY nullbytes
1115#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1116#define STRINGLIB_MUTABLE 1
Christian Heimes7d4c3172008-08-22 19:47:25 +00001117#define FROM_BYTEARRAY 1
Christian Heimes44720832008-05-26 13:01:01 +00001118
1119#include "stringlib/fastsearch.h"
1120#include "stringlib/count.h"
1121#include "stringlib/find.h"
1122#include "stringlib/partition.h"
1123#include "stringlib/ctype.h"
1124#include "stringlib/transmogrify.h"
1125
1126
1127/* The following Py_LOCAL_INLINE and Py_LOCAL functions
1128were copied from the old char* style string object. */
1129
1130Py_LOCAL_INLINE(void)
1131_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
1132{
1133 if (*end > len)
1134 *end = len;
1135 else if (*end < 0)
1136 *end += len;
1137 if (*end < 0)
1138 *end = 0;
1139 if (*start < 0)
1140 *start += len;
1141 if (*start < 0)
1142 *start = 0;
1143}
1144
1145
1146Py_LOCAL_INLINE(Py_ssize_t)
1147bytes_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
1148{
1149 PyObject *subobj;
1150 Py_buffer subbuf;
1151 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1152 Py_ssize_t res;
1153
1154 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj,
1155 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1156 return -2;
1157 if (_getbuffer(subobj, &subbuf) < 0)
1158 return -2;
1159 if (dir > 0)
1160 res = stringlib_find_slice(
1161 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1162 subbuf.buf, subbuf.len, start, end);
1163 else
1164 res = stringlib_rfind_slice(
1165 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1166 subbuf.buf, subbuf.len, start, end);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001167 PyBuffer_Release(&subbuf);
Christian Heimes44720832008-05-26 13:01:01 +00001168 return res;
1169}
1170
1171PyDoc_STRVAR(find__doc__,
1172"B.find(sub [,start [,end]]) -> int\n\
1173\n\
1174Return the lowest index in B where subsection sub is found,\n\
1175such that sub is contained within s[start,end]. Optional\n\
1176arguments start and end are interpreted as in slice notation.\n\
1177\n\
1178Return -1 on failure.");
1179
1180static PyObject *
1181bytes_find(PyByteArrayObject *self, PyObject *args)
1182{
1183 Py_ssize_t result = bytes_find_internal(self, args, +1);
1184 if (result == -2)
1185 return NULL;
1186 return PyInt_FromSsize_t(result);
1187}
1188
1189PyDoc_STRVAR(count__doc__,
1190"B.count(sub [,start [,end]]) -> int\n\
1191\n\
1192Return the number of non-overlapping occurrences of subsection sub in\n\
1193bytes B[start:end]. Optional arguments start and end are interpreted\n\
1194as in slice notation.");
1195
1196static PyObject *
1197bytes_count(PyByteArrayObject *self, PyObject *args)
1198{
1199 PyObject *sub_obj;
1200 const char *str = PyByteArray_AS_STRING(self);
1201 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
1202 Py_buffer vsub;
1203 PyObject *count_obj;
1204
1205 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
1206 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1207 return NULL;
1208
1209 if (_getbuffer(sub_obj, &vsub) < 0)
1210 return NULL;
1211
1212 _adjust_indices(&start, &end, PyByteArray_GET_SIZE(self));
1213
1214 count_obj = PyInt_FromSsize_t(
1215 stringlib_count(str + start, end - start, vsub.buf, vsub.len)
1216 );
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001217 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00001218 return count_obj;
1219}
1220
1221
1222PyDoc_STRVAR(index__doc__,
1223"B.index(sub [,start [,end]]) -> int\n\
1224\n\
1225Like B.find() but raise ValueError when the subsection is not found.");
1226
1227static PyObject *
1228bytes_index(PyByteArrayObject *self, PyObject *args)
1229{
1230 Py_ssize_t result = bytes_find_internal(self, args, +1);
1231 if (result == -2)
1232 return NULL;
1233 if (result == -1) {
1234 PyErr_SetString(PyExc_ValueError,
1235 "subsection not found");
1236 return NULL;
1237 }
1238 return PyInt_FromSsize_t(result);
1239}
1240
1241
1242PyDoc_STRVAR(rfind__doc__,
1243"B.rfind(sub [,start [,end]]) -> int\n\
1244\n\
1245Return the highest index in B where subsection sub is found,\n\
1246such that sub is contained within s[start,end]. Optional\n\
1247arguments start and end are interpreted as in slice notation.\n\
1248\n\
1249Return -1 on failure.");
1250
1251static PyObject *
1252bytes_rfind(PyByteArrayObject *self, PyObject *args)
1253{
1254 Py_ssize_t result = bytes_find_internal(self, args, -1);
1255 if (result == -2)
1256 return NULL;
1257 return PyInt_FromSsize_t(result);
1258}
1259
1260
1261PyDoc_STRVAR(rindex__doc__,
1262"B.rindex(sub [,start [,end]]) -> int\n\
1263\n\
1264Like B.rfind() but raise ValueError when the subsection is not found.");
1265
1266static PyObject *
1267bytes_rindex(PyByteArrayObject *self, PyObject *args)
1268{
1269 Py_ssize_t result = bytes_find_internal(self, args, -1);
1270 if (result == -2)
1271 return NULL;
1272 if (result == -1) {
1273 PyErr_SetString(PyExc_ValueError,
1274 "subsection not found");
1275 return NULL;
1276 }
1277 return PyInt_FromSsize_t(result);
1278}
1279
1280
1281static int
1282bytes_contains(PyObject *self, PyObject *arg)
1283{
1284 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1285 if (ival == -1 && PyErr_Occurred()) {
1286 Py_buffer varg;
1287 int pos;
1288 PyErr_Clear();
1289 if (_getbuffer(arg, &varg) < 0)
1290 return -1;
1291 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1292 varg.buf, varg.len, 0);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001293 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00001294 return pos >= 0;
1295 }
1296 if (ival < 0 || ival >= 256) {
1297 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1298 return -1;
1299 }
1300
1301 return memchr(PyByteArray_AS_STRING(self), ival, Py_SIZE(self)) != NULL;
1302}
1303
1304
1305/* Matches the end (direction >= 0) or start (direction < 0) of self
1306 * against substr, using the start and end arguments. Returns
1307 * -1 on error, 0 if not found and 1 if found.
1308 */
1309Py_LOCAL(int)
1310_bytes_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
1311 Py_ssize_t end, int direction)
1312{
1313 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1314 const char* str;
1315 Py_buffer vsubstr;
1316 int rv = 0;
1317
1318 str = PyByteArray_AS_STRING(self);
1319
1320 if (_getbuffer(substr, &vsubstr) < 0)
1321 return -1;
1322
1323 _adjust_indices(&start, &end, len);
1324
1325 if (direction < 0) {
1326 /* startswith */
1327 if (start+vsubstr.len > len) {
1328 goto done;
1329 }
1330 } else {
1331 /* endswith */
1332 if (end-start < vsubstr.len || start > len) {
1333 goto done;
1334 }
1335
1336 if (end-vsubstr.len > start)
1337 start = end - vsubstr.len;
1338 }
1339 if (end-start >= vsubstr.len)
1340 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1341
1342done:
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001343 PyBuffer_Release(&vsubstr);
Christian Heimes44720832008-05-26 13:01:01 +00001344 return rv;
1345}
1346
1347
1348PyDoc_STRVAR(startswith__doc__,
1349"B.startswith(prefix [,start [,end]]) -> bool\n\
1350\n\
1351Return True if B starts with the specified prefix, False otherwise.\n\
1352With optional start, test B beginning at that position.\n\
1353With optional end, stop comparing B at that position.\n\
1354prefix can also be a tuple of strings to try.");
1355
1356static PyObject *
1357bytes_startswith(PyByteArrayObject *self, PyObject *args)
1358{
1359 Py_ssize_t start = 0;
1360 Py_ssize_t end = PY_SSIZE_T_MAX;
1361 PyObject *subobj;
1362 int result;
1363
1364 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
1365 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1366 return NULL;
1367 if (PyTuple_Check(subobj)) {
1368 Py_ssize_t i;
1369 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1370 result = _bytes_tailmatch(self,
1371 PyTuple_GET_ITEM(subobj, i),
1372 start, end, -1);
1373 if (result == -1)
1374 return NULL;
1375 else if (result) {
1376 Py_RETURN_TRUE;
1377 }
1378 }
1379 Py_RETURN_FALSE;
1380 }
1381 result = _bytes_tailmatch(self, subobj, start, end, -1);
1382 if (result == -1)
1383 return NULL;
1384 else
1385 return PyBool_FromLong(result);
1386}
1387
1388PyDoc_STRVAR(endswith__doc__,
1389"B.endswith(suffix [,start [,end]]) -> bool\n\
1390\n\
1391Return True if B ends with the specified suffix, False otherwise.\n\
1392With optional start, test B beginning at that position.\n\
1393With optional end, stop comparing B at that position.\n\
1394suffix can also be a tuple of strings to try.");
1395
1396static PyObject *
1397bytes_endswith(PyByteArrayObject *self, PyObject *args)
1398{
1399 Py_ssize_t start = 0;
1400 Py_ssize_t end = PY_SSIZE_T_MAX;
1401 PyObject *subobj;
1402 int result;
1403
1404 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
1405 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1406 return NULL;
1407 if (PyTuple_Check(subobj)) {
1408 Py_ssize_t i;
1409 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1410 result = _bytes_tailmatch(self,
1411 PyTuple_GET_ITEM(subobj, i),
1412 start, end, +1);
1413 if (result == -1)
1414 return NULL;
1415 else if (result) {
1416 Py_RETURN_TRUE;
1417 }
1418 }
1419 Py_RETURN_FALSE;
1420 }
1421 result = _bytes_tailmatch(self, subobj, start, end, +1);
1422 if (result == -1)
1423 return NULL;
1424 else
1425 return PyBool_FromLong(result);
1426}
1427
1428
1429PyDoc_STRVAR(translate__doc__,
1430"B.translate(table[, deletechars]) -> bytearray\n\
1431\n\
1432Return a copy of B, where all characters occurring in the\n\
1433optional argument deletechars are removed, and the remaining\n\
1434characters have been mapped through the given translation\n\
1435table, which must be a bytes object of length 256.");
1436
1437static PyObject *
1438bytes_translate(PyByteArrayObject *self, PyObject *args)
1439{
1440 register char *input, *output;
1441 register const char *table;
Benjamin Peterson866eba92008-11-19 22:05:53 +00001442 register Py_ssize_t i, c;
Christian Heimes44720832008-05-26 13:01:01 +00001443 PyObject *input_obj = (PyObject*)self;
1444 const char *output_start;
1445 Py_ssize_t inlen;
1446 PyObject *result;
1447 int trans_table[256];
1448 PyObject *tableobj, *delobj = NULL;
1449 Py_buffer vtable, vdel;
1450
1451 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
1452 &tableobj, &delobj))
1453 return NULL;
1454
1455 if (_getbuffer(tableobj, &vtable) < 0)
1456 return NULL;
1457
1458 if (vtable.len != 256) {
1459 PyErr_SetString(PyExc_ValueError,
1460 "translation table must be 256 characters long");
1461 result = NULL;
1462 goto done;
1463 }
1464
1465 if (delobj != NULL) {
1466 if (_getbuffer(delobj, &vdel) < 0) {
1467 result = NULL;
Georg Brandlef29f862008-12-28 11:55:24 +00001468 delobj = NULL; /* don't try to release vdel buffer on exit */
Christian Heimes44720832008-05-26 13:01:01 +00001469 goto done;
1470 }
1471 }
1472 else {
1473 vdel.buf = NULL;
1474 vdel.len = 0;
1475 }
1476
1477 table = (const char *)vtable.buf;
1478 inlen = PyByteArray_GET_SIZE(input_obj);
1479 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1480 if (result == NULL)
1481 goto done;
1482 output_start = output = PyByteArray_AsString(result);
1483 input = PyByteArray_AS_STRING(input_obj);
1484
1485 if (vdel.len == 0) {
1486 /* If no deletions are required, use faster code */
1487 for (i = inlen; --i >= 0; ) {
1488 c = Py_CHARMASK(*input++);
Benjamin Peterson866eba92008-11-19 22:05:53 +00001489 *output++ = table[c];
Christian Heimes44720832008-05-26 13:01:01 +00001490 }
Christian Heimes44720832008-05-26 13:01:01 +00001491 goto done;
1492 }
Antoine Pitrou599db7f2008-12-07 00:07:51 +00001493
Christian Heimes44720832008-05-26 13:01:01 +00001494 for (i = 0; i < 256; i++)
1495 trans_table[i] = Py_CHARMASK(table[i]);
1496
1497 for (i = 0; i < vdel.len; i++)
1498 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1499
1500 for (i = inlen; --i >= 0; ) {
1501 c = Py_CHARMASK(*input++);
1502 if (trans_table[c] != -1)
1503 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1504 continue;
Christian Heimes44720832008-05-26 13:01:01 +00001505 }
1506 /* Fix the size of the resulting string */
1507 if (inlen > 0)
1508 PyByteArray_Resize(result, output - output_start);
1509
1510done:
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001511 PyBuffer_Release(&vtable);
Christian Heimes44720832008-05-26 13:01:01 +00001512 if (delobj != NULL)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001513 PyBuffer_Release(&vdel);
Christian Heimes44720832008-05-26 13:01:01 +00001514 return result;
1515}
1516
1517
1518#define FORWARD 1
1519#define REVERSE -1
1520
1521/* find and count characters and substrings */
1522
1523#define findchar(target, target_len, c) \
1524 ((char *)memchr((const void *)(target), c, target_len))
1525
1526/* Don't call if length < 2 */
1527#define Py_STRING_MATCH(target, offset, pattern, length) \
1528 (target[offset] == pattern[0] && \
1529 target[offset+length-1] == pattern[length-1] && \
1530 !memcmp(target+offset+1, pattern+1, length-2) )
1531
1532
Benjamin Peterson866eba92008-11-19 22:05:53 +00001533/* Bytes ops must return a string, create a copy */
Christian Heimes44720832008-05-26 13:01:01 +00001534Py_LOCAL(PyByteArrayObject *)
1535return_self(PyByteArrayObject *self)
1536{
Christian Heimes44720832008-05-26 13:01:01 +00001537 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 }
Antoine Pitrou599db7f2008-12-07 00:07:51 +00002750 if (!_canresize(self))
2751 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00002752
2753 value = self->ob_bytes[where];
2754 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where);
2755 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2756 return NULL;
2757
2758 return PyInt_FromLong(value);
2759}
2760
2761PyDoc_STRVAR(remove__doc__,
2762"B.remove(int) -> None\n\
2763\n\
2764Remove the first occurance of a value in B.");
2765static PyObject *
2766bytes_remove(PyByteArrayObject *self, PyObject *arg)
2767{
2768 int value;
2769 Py_ssize_t where, n = Py_SIZE(self);
2770
2771 if (! _getbytevalue(arg, &value))
2772 return NULL;
2773
2774 for (where = 0; where < n; where++) {
2775 if (self->ob_bytes[where] == value)
2776 break;
2777 }
2778 if (where == n) {
2779 PyErr_SetString(PyExc_ValueError, "value not found in bytes");
2780 return NULL;
2781 }
Antoine Pitrou599db7f2008-12-07 00:07:51 +00002782 if (!_canresize(self))
2783 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00002784
2785 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where);
2786 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2787 return NULL;
2788
2789 Py_RETURN_NONE;
2790}
2791
2792/* XXX These two helpers could be optimized if argsize == 1 */
2793
2794static Py_ssize_t
2795lstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
2796 void *argptr, Py_ssize_t argsize)
2797{
2798 Py_ssize_t i = 0;
2799 while (i < mysize && memchr(argptr, myptr[i], argsize))
2800 i++;
2801 return i;
2802}
2803
2804static Py_ssize_t
2805rstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
2806 void *argptr, Py_ssize_t argsize)
2807{
2808 Py_ssize_t i = mysize - 1;
2809 while (i >= 0 && memchr(argptr, myptr[i], argsize))
2810 i--;
2811 return i + 1;
2812}
2813
2814PyDoc_STRVAR(strip__doc__,
2815"B.strip([bytes]) -> bytearray\n\
2816\n\
2817Strip leading and trailing bytes contained in the argument.\n\
2818If the argument is omitted, strip ASCII whitespace.");
2819static PyObject *
2820bytes_strip(PyByteArrayObject *self, PyObject *args)
2821{
2822 Py_ssize_t left, right, mysize, argsize;
2823 void *myptr, *argptr;
2824 PyObject *arg = Py_None;
2825 Py_buffer varg;
2826 if (!PyArg_ParseTuple(args, "|O:strip", &arg))
2827 return NULL;
2828 if (arg == Py_None) {
2829 argptr = "\t\n\r\f\v ";
2830 argsize = 6;
2831 }
2832 else {
2833 if (_getbuffer(arg, &varg) < 0)
2834 return NULL;
2835 argptr = varg.buf;
2836 argsize = varg.len;
2837 }
2838 myptr = self->ob_bytes;
2839 mysize = Py_SIZE(self);
2840 left = lstrip_helper(myptr, mysize, argptr, argsize);
2841 if (left == mysize)
2842 right = left;
2843 else
2844 right = rstrip_helper(myptr, mysize, argptr, argsize);
2845 if (arg != Py_None)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002846 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00002847 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2848}
2849
2850PyDoc_STRVAR(lstrip__doc__,
2851"B.lstrip([bytes]) -> bytearray\n\
2852\n\
2853Strip leading bytes contained in the argument.\n\
2854If the argument is omitted, strip leading ASCII whitespace.");
2855static PyObject *
2856bytes_lstrip(PyByteArrayObject *self, PyObject *args)
2857{
2858 Py_ssize_t left, right, mysize, argsize;
2859 void *myptr, *argptr;
2860 PyObject *arg = Py_None;
2861 Py_buffer varg;
2862 if (!PyArg_ParseTuple(args, "|O:lstrip", &arg))
2863 return NULL;
2864 if (arg == Py_None) {
2865 argptr = "\t\n\r\f\v ";
2866 argsize = 6;
2867 }
2868 else {
2869 if (_getbuffer(arg, &varg) < 0)
2870 return NULL;
2871 argptr = varg.buf;
2872 argsize = varg.len;
2873 }
2874 myptr = self->ob_bytes;
2875 mysize = Py_SIZE(self);
2876 left = lstrip_helper(myptr, mysize, argptr, argsize);
2877 right = mysize;
2878 if (arg != Py_None)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002879 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00002880 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2881}
2882
2883PyDoc_STRVAR(rstrip__doc__,
2884"B.rstrip([bytes]) -> bytearray\n\
2885\n\
2886Strip trailing bytes contained in the argument.\n\
2887If the argument is omitted, strip trailing ASCII whitespace.");
2888static PyObject *
2889bytes_rstrip(PyByteArrayObject *self, PyObject *args)
2890{
2891 Py_ssize_t left, right, mysize, argsize;
2892 void *myptr, *argptr;
2893 PyObject *arg = Py_None;
2894 Py_buffer varg;
2895 if (!PyArg_ParseTuple(args, "|O:rstrip", &arg))
2896 return NULL;
2897 if (arg == Py_None) {
2898 argptr = "\t\n\r\f\v ";
2899 argsize = 6;
2900 }
2901 else {
2902 if (_getbuffer(arg, &varg) < 0)
2903 return NULL;
2904 argptr = varg.buf;
2905 argsize = varg.len;
2906 }
2907 myptr = self->ob_bytes;
2908 mysize = Py_SIZE(self);
2909 left = 0;
2910 right = rstrip_helper(myptr, mysize, argptr, argsize);
2911 if (arg != Py_None)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002912 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00002913 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2914}
2915
2916PyDoc_STRVAR(decode_doc,
2917"B.decode([encoding[, errors]]) -> unicode object.\n\
2918\n\
2919Decodes B using the codec registered for encoding. encoding defaults\n\
2920to the default encoding. errors may be given to set a different error\n\
2921handling scheme. Default is 'strict' meaning that encoding errors raise\n\
2922a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
2923as well as any other name registered with codecs.register_error that is\n\
2924able to handle UnicodeDecodeErrors.");
2925
2926static PyObject *
2927bytes_decode(PyObject *self, PyObject *args)
2928{
2929 const char *encoding = NULL;
2930 const char *errors = NULL;
2931
2932 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
2933 return NULL;
2934 if (encoding == NULL)
2935 encoding = PyUnicode_GetDefaultEncoding();
2936 return PyCodec_Decode(self, encoding, errors);
2937}
2938
2939PyDoc_STRVAR(alloc_doc,
2940"B.__alloc__() -> int\n\
2941\n\
2942Returns the number of bytes actually allocated.");
2943
2944static PyObject *
2945bytes_alloc(PyByteArrayObject *self)
2946{
2947 return PyInt_FromSsize_t(self->ob_alloc);
2948}
2949
2950PyDoc_STRVAR(join_doc,
2951"B.join(iterable_of_bytes) -> bytes\n\
2952\n\
2953Concatenates any number of bytearray objects, with B in between each pair.");
2954
2955static PyObject *
2956bytes_join(PyByteArrayObject *self, PyObject *it)
2957{
2958 PyObject *seq;
2959 Py_ssize_t mysize = Py_SIZE(self);
2960 Py_ssize_t i;
2961 Py_ssize_t n;
2962 PyObject **items;
2963 Py_ssize_t totalsize = 0;
2964 PyObject *result;
2965 char *dest;
2966
2967 seq = PySequence_Fast(it, "can only join an iterable");
2968 if (seq == NULL)
2969 return NULL;
2970 n = PySequence_Fast_GET_SIZE(seq);
2971 items = PySequence_Fast_ITEMS(seq);
2972
2973 /* Compute the total size, and check that they are all bytes */
2974 /* XXX Shouldn't we use _getbuffer() on these items instead? */
2975 for (i = 0; i < n; i++) {
2976 PyObject *obj = items[i];
2977 if (!PyByteArray_Check(obj) && !PyBytes_Check(obj)) {
2978 PyErr_Format(PyExc_TypeError,
2979 "can only join an iterable of bytes "
2980 "(item %ld has type '%.100s')",
2981 /* XXX %ld isn't right on Win64 */
2982 (long)i, Py_TYPE(obj)->tp_name);
2983 goto error;
2984 }
2985 if (i > 0)
2986 totalsize += mysize;
2987 totalsize += Py_SIZE(obj);
2988 if (totalsize < 0) {
2989 PyErr_NoMemory();
2990 goto error;
2991 }
2992 }
2993
2994 /* Allocate the result, and copy the bytes */
2995 result = PyByteArray_FromStringAndSize(NULL, totalsize);
2996 if (result == NULL)
2997 goto error;
2998 dest = PyByteArray_AS_STRING(result);
2999 for (i = 0; i < n; i++) {
3000 PyObject *obj = items[i];
3001 Py_ssize_t size = Py_SIZE(obj);
3002 char *buf;
3003 if (PyByteArray_Check(obj))
3004 buf = PyByteArray_AS_STRING(obj);
3005 else
3006 buf = PyBytes_AS_STRING(obj);
3007 if (i) {
3008 memcpy(dest, self->ob_bytes, mysize);
3009 dest += mysize;
3010 }
3011 memcpy(dest, buf, size);
3012 dest += size;
3013 }
3014
3015 /* Done */
3016 Py_DECREF(seq);
3017 return result;
3018
3019 /* Error handling */
3020 error:
3021 Py_DECREF(seq);
3022 return NULL;
3023}
3024
3025PyDoc_STRVAR(fromhex_doc,
3026"bytearray.fromhex(string) -> bytearray\n\
3027\n\
3028Create a bytearray object from a string of hexadecimal numbers.\n\
3029Spaces between two numbers are accepted.\n\
3030Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
3031
3032static int
3033hex_digit_to_int(Py_UNICODE c)
3034{
3035 if (c >= 128)
3036 return -1;
3037 if (ISDIGIT(c))
3038 return c - '0';
3039 else {
3040 if (ISUPPER(c))
3041 c = TOLOWER(c);
3042 if (c >= 'a' && c <= 'f')
3043 return c - 'a' + 10;
3044 }
3045 return -1;
3046}
3047
3048static PyObject *
3049bytes_fromhex(PyObject *cls, PyObject *args)
3050{
3051 PyObject *newbytes, *hexobj;
3052 char *buf;
3053 Py_UNICODE *hex;
3054 Py_ssize_t hexlen, byteslen, i, j;
3055 int top, bot;
3056
3057 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
3058 return NULL;
3059 assert(PyUnicode_Check(hexobj));
3060 hexlen = PyUnicode_GET_SIZE(hexobj);
3061 hex = PyUnicode_AS_UNICODE(hexobj);
3062 byteslen = hexlen/2; /* This overestimates if there are spaces */
3063 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
3064 if (!newbytes)
3065 return NULL;
3066 buf = PyByteArray_AS_STRING(newbytes);
3067 for (i = j = 0; i < hexlen; i += 2) {
3068 /* skip over spaces in the input */
3069 while (hex[i] == ' ')
3070 i++;
3071 if (i >= hexlen)
3072 break;
3073 top = hex_digit_to_int(hex[i]);
3074 bot = hex_digit_to_int(hex[i+1]);
3075 if (top == -1 || bot == -1) {
3076 PyErr_Format(PyExc_ValueError,
3077 "non-hexadecimal number found in "
3078 "fromhex() arg at position %zd", i);
3079 goto error;
3080 }
3081 buf[j++] = (top << 4) + bot;
3082 }
3083 if (PyByteArray_Resize(newbytes, j) < 0)
3084 goto error;
3085 return newbytes;
3086
3087 error:
3088 Py_DECREF(newbytes);
3089 return NULL;
3090}
3091
3092PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3093
3094static PyObject *
3095bytes_reduce(PyByteArrayObject *self)
3096{
3097 PyObject *latin1, *dict;
3098 if (self->ob_bytes)
3099 latin1 = PyUnicode_DecodeLatin1(self->ob_bytes,
3100 Py_SIZE(self), NULL);
3101 else
3102 latin1 = PyUnicode_FromString("");
3103
3104 dict = PyObject_GetAttrString((PyObject *)self, "__dict__");
3105 if (dict == NULL) {
3106 PyErr_Clear();
3107 dict = Py_None;
3108 Py_INCREF(dict);
3109 }
3110
3111 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
3112}
3113
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003114PyDoc_STRVAR(sizeof_doc,
3115"B.__sizeof__() -> int\n\
3116 \n\
3117Returns the size of B in memory, in bytes");
3118static PyObject *
3119bytes_sizeof(PyByteArrayObject *self)
3120{
3121 Py_ssize_t res;
3122
3123 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
3124 return PyInt_FromSsize_t(res);
3125}
3126
Christian Heimes44720832008-05-26 13:01:01 +00003127static PySequenceMethods bytes_as_sequence = {
3128 (lenfunc)bytes_length, /* sq_length */
3129 (binaryfunc)PyByteArray_Concat, /* sq_concat */
3130 (ssizeargfunc)bytes_repeat, /* sq_repeat */
3131 (ssizeargfunc)bytes_getitem, /* sq_item */
3132 0, /* sq_slice */
3133 (ssizeobjargproc)bytes_setitem, /* sq_ass_item */
3134 0, /* sq_ass_slice */
3135 (objobjproc)bytes_contains, /* sq_contains */
3136 (binaryfunc)bytes_iconcat, /* sq_inplace_concat */
3137 (ssizeargfunc)bytes_irepeat, /* sq_inplace_repeat */
3138};
3139
3140static PyMappingMethods bytes_as_mapping = {
3141 (lenfunc)bytes_length,
3142 (binaryfunc)bytes_subscript,
3143 (objobjargproc)bytes_ass_subscript,
3144};
3145
3146static PyBufferProcs bytes_as_buffer = {
3147 (readbufferproc)bytes_buffer_getreadbuf,
3148 (writebufferproc)bytes_buffer_getwritebuf,
3149 (segcountproc)bytes_buffer_getsegcount,
3150 (charbufferproc)bytes_buffer_getcharbuf,
3151 (getbufferproc)bytes_getbuffer,
3152 (releasebufferproc)bytes_releasebuffer,
3153};
3154
3155static PyMethodDef
3156bytes_methods[] = {
3157 {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc},
3158 {"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc},
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003159 {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, sizeof_doc},
Christian Heimes44720832008-05-26 13:01:01 +00003160 {"append", (PyCFunction)bytes_append, METH_O, append__doc__},
3161 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
3162 _Py_capitalize__doc__},
3163 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
3164 {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__},
3165 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc},
3166 {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, endswith__doc__},
3167 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS,
3168 expandtabs__doc__},
3169 {"extend", (PyCFunction)bytes_extend, METH_O, extend__doc__},
3170 {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__},
3171 {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS,
3172 fromhex_doc},
3173 {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__},
3174 {"insert", (PyCFunction)bytes_insert, METH_VARARGS, insert__doc__},
3175 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
3176 _Py_isalnum__doc__},
3177 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
3178 _Py_isalpha__doc__},
3179 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
3180 _Py_isdigit__doc__},
3181 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
3182 _Py_islower__doc__},
3183 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
3184 _Py_isspace__doc__},
3185 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
3186 _Py_istitle__doc__},
3187 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
3188 _Py_isupper__doc__},
3189 {"join", (PyCFunction)bytes_join, METH_O, join_doc},
3190 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
3191 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
3192 {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__},
3193 {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__},
3194 {"pop", (PyCFunction)bytes_pop, METH_VARARGS, pop__doc__},
3195 {"remove", (PyCFunction)bytes_remove, METH_O, remove__doc__},
3196 {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__},
3197 {"reverse", (PyCFunction)bytes_reverse, METH_NOARGS, reverse__doc__},
3198 {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__},
3199 {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__},
3200 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
3201 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, rpartition__doc__},
3202 {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__},
3203 {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__},
3204 {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__},
3205 {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS,
3206 splitlines__doc__},
3207 {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS ,
3208 startswith__doc__},
3209 {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__},
3210 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
3211 _Py_swapcase__doc__},
3212 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
3213 {"translate", (PyCFunction)bytes_translate, METH_VARARGS,
3214 translate__doc__},
3215 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
3216 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
3217 {NULL}
3218};
3219
3220PyDoc_STRVAR(bytes_doc,
3221"bytearray(iterable_of_ints) -> bytearray.\n\
3222bytearray(string, encoding[, errors]) -> bytearray.\n\
3223bytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray.\n\
3224bytearray(memory_view) -> bytearray.\n\
3225\n\
3226Construct an mutable bytearray object from:\n\
3227 - an iterable yielding integers in range(256)\n\
3228 - a text string encoded using the specified encoding\n\
3229 - a bytes or a bytearray object\n\
3230 - any object implementing the buffer API.\n\
3231\n\
3232bytearray(int) -> bytearray.\n\
3233\n\
3234Construct a zero-initialized bytearray of the given length.");
3235
3236
3237static PyObject *bytes_iter(PyObject *seq);
3238
3239PyTypeObject PyByteArray_Type = {
3240 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3241 "bytearray",
3242 sizeof(PyByteArrayObject),
3243 0,
3244 (destructor)bytes_dealloc, /* tp_dealloc */
3245 0, /* tp_print */
3246 0, /* tp_getattr */
3247 0, /* tp_setattr */
3248 0, /* tp_compare */
3249 (reprfunc)bytes_repr, /* tp_repr */
3250 0, /* tp_as_number */
3251 &bytes_as_sequence, /* tp_as_sequence */
3252 &bytes_as_mapping, /* tp_as_mapping */
3253 0, /* tp_hash */
3254 0, /* tp_call */
3255 bytes_str, /* tp_str */
3256 PyObject_GenericGetAttr, /* tp_getattro */
3257 0, /* tp_setattro */
3258 &bytes_as_buffer, /* tp_as_buffer */
3259 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
3260 Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */
3261 bytes_doc, /* tp_doc */
3262 0, /* tp_traverse */
3263 0, /* tp_clear */
3264 (richcmpfunc)bytes_richcompare, /* tp_richcompare */
3265 0, /* tp_weaklistoffset */
3266 bytes_iter, /* tp_iter */
3267 0, /* tp_iternext */
3268 bytes_methods, /* tp_methods */
3269 0, /* tp_members */
3270 0, /* tp_getset */
3271 0, /* tp_base */
3272 0, /* tp_dict */
3273 0, /* tp_descr_get */
3274 0, /* tp_descr_set */
3275 0, /* tp_dictoffset */
3276 (initproc)bytes_init, /* tp_init */
3277 PyType_GenericAlloc, /* tp_alloc */
3278 PyType_GenericNew, /* tp_new */
3279 PyObject_Del, /* tp_free */
3280};
3281
3282/*********************** Bytes Iterator ****************************/
3283
3284typedef struct {
3285 PyObject_HEAD
3286 Py_ssize_t it_index;
3287 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
3288} bytesiterobject;
3289
3290static void
3291bytesiter_dealloc(bytesiterobject *it)
3292{
3293 _PyObject_GC_UNTRACK(it);
3294 Py_XDECREF(it->it_seq);
3295 PyObject_GC_Del(it);
3296}
3297
3298static int
3299bytesiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
3300{
3301 Py_VISIT(it->it_seq);
3302 return 0;
3303}
3304
3305static PyObject *
3306bytesiter_next(bytesiterobject *it)
3307{
3308 PyByteArrayObject *seq;
3309 PyObject *item;
3310
3311 assert(it != NULL);
3312 seq = it->it_seq;
3313 if (seq == NULL)
3314 return NULL;
3315 assert(PyByteArray_Check(seq));
3316
3317 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
3318 item = PyInt_FromLong(
3319 (unsigned char)seq->ob_bytes[it->it_index]);
3320 if (item != NULL)
3321 ++it->it_index;
3322 return item;
3323 }
3324
3325 Py_DECREF(seq);
3326 it->it_seq = NULL;
3327 return NULL;
3328}
3329
3330static PyObject *
3331bytesiter_length_hint(bytesiterobject *it)
3332{
3333 Py_ssize_t len = 0;
3334 if (it->it_seq)
3335 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
3336 return PyInt_FromSsize_t(len);
3337}
3338
3339PyDoc_STRVAR(length_hint_doc,
3340 "Private method returning an estimate of len(list(it)).");
3341
3342static PyMethodDef bytesiter_methods[] = {
3343 {"__length_hint__", (PyCFunction)bytesiter_length_hint, METH_NOARGS,
3344 length_hint_doc},
3345 {NULL, NULL} /* sentinel */
3346};
3347
3348PyTypeObject PyByteArrayIter_Type = {
3349 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3350 "bytearray_iterator", /* tp_name */
3351 sizeof(bytesiterobject), /* tp_basicsize */
3352 0, /* tp_itemsize */
3353 /* methods */
3354 (destructor)bytesiter_dealloc, /* tp_dealloc */
3355 0, /* tp_print */
3356 0, /* tp_getattr */
3357 0, /* tp_setattr */
3358 0, /* tp_compare */
3359 0, /* tp_repr */
3360 0, /* tp_as_number */
3361 0, /* tp_as_sequence */
3362 0, /* tp_as_mapping */
3363 0, /* tp_hash */
3364 0, /* tp_call */
3365 0, /* tp_str */
3366 PyObject_GenericGetAttr, /* tp_getattro */
3367 0, /* tp_setattro */
3368 0, /* tp_as_buffer */
3369 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3370 0, /* tp_doc */
3371 (traverseproc)bytesiter_traverse, /* tp_traverse */
3372 0, /* tp_clear */
3373 0, /* tp_richcompare */
3374 0, /* tp_weaklistoffset */
3375 PyObject_SelfIter, /* tp_iter */
3376 (iternextfunc)bytesiter_next, /* tp_iternext */
3377 bytesiter_methods, /* tp_methods */
3378 0,
3379};
3380
3381static PyObject *
3382bytes_iter(PyObject *seq)
3383{
3384 bytesiterobject *it;
3385
3386 if (!PyByteArray_Check(seq)) {
3387 PyErr_BadInternalCall();
3388 return NULL;
3389 }
3390 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3391 if (it == NULL)
3392 return NULL;
3393 it->it_index = 0;
3394 Py_INCREF(seq);
3395 it->it_seq = (PyByteArrayObject *)seq;
3396 _PyObject_GC_TRACK(it);
3397 return (PyObject *)it;
3398}