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