blob: a634cff27beb0ec2b4c3242da519a047e95f5d64 [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
716 if (cur + step >= PyByteArray_GET_SIZE(self))
717 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;
724 if (cur < PyByteArray_GET_SIZE(self)) {
725 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? */
833 count = PyNumber_AsSsize_t(arg, PyExc_ValueError);
834 if (count == -1 && PyErr_Occurred())
835 PyErr_Clear();
836 else {
837 if (count < 0) {
838 PyErr_SetString(PyExc_ValueError, "negative count");
839 return -1;
840 }
841 if (count > 0) {
842 if (PyByteArray_Resize((PyObject *)self, count))
843 return -1;
844 memset(self->ob_bytes, 0, count);
845 }
846 return 0;
847 }
848
849 /* Use the buffer API */
850 if (PyObject_CheckBuffer(arg)) {
851 Py_ssize_t size;
852 Py_buffer view;
853 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
854 return -1;
855 size = view.len;
856 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
857 if (PyBuffer_ToContiguous(self->ob_bytes, &view, size, 'C') < 0)
858 goto fail;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000859 PyBuffer_Release(&view);
Christian Heimes44720832008-05-26 13:01:01 +0000860 return 0;
861 fail:
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000862 PyBuffer_Release(&view);
Christian Heimes44720832008-05-26 13:01:01 +0000863 return -1;
864 }
865
866 /* XXX Optimize this if the arguments is a list, tuple */
867
868 /* Get the iterator */
869 it = PyObject_GetIter(arg);
870 if (it == NULL)
871 return -1;
872 iternext = *Py_TYPE(it)->tp_iternext;
873
874 /* Run the iterator to exhaustion */
875 for (;;) {
876 PyObject *item;
Georg Brandl3e758462008-07-16 23:10:05 +0000877 int rc, value;
Christian Heimes44720832008-05-26 13:01:01 +0000878
879 /* Get the next item */
880 item = iternext(it);
881 if (item == NULL) {
882 if (PyErr_Occurred()) {
883 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
884 goto error;
885 PyErr_Clear();
886 }
887 break;
888 }
889
890 /* Interpret it as an int (__index__) */
Georg Brandl3e758462008-07-16 23:10:05 +0000891 rc = _getbytevalue(item, &value);
Christian Heimes44720832008-05-26 13:01:01 +0000892 Py_DECREF(item);
Georg Brandl3e758462008-07-16 23:10:05 +0000893 if (!rc)
Christian Heimes44720832008-05-26 13:01:01 +0000894 goto error;
895
Christian Heimes44720832008-05-26 13:01:01 +0000896 /* Append the byte */
897 if (Py_SIZE(self) < self->ob_alloc)
898 Py_SIZE(self)++;
899 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
900 goto error;
901 self->ob_bytes[Py_SIZE(self)-1] = value;
902 }
903
904 /* Clean up and return success */
905 Py_DECREF(it);
906 return 0;
907
908 error:
909 /* Error handling when it != NULL */
910 Py_DECREF(it);
911 return -1;
912}
913
914/* Mostly copied from string_repr, but without the
915 "smart quote" functionality. */
916static PyObject *
917bytes_repr(PyByteArrayObject *self)
918{
919 static const char *hexdigits = "0123456789abcdef";
920 const char *quote_prefix = "bytearray(b";
921 const char *quote_postfix = ")";
922 Py_ssize_t length = Py_SIZE(self);
923 /* 14 == strlen(quote_prefix) + 2 + strlen(quote_postfix) */
924 size_t newsize = 14 + 4 * length;
925 PyObject *v;
926 if (newsize > PY_SSIZE_T_MAX || newsize / 4 - 3 != length) {
927 PyErr_SetString(PyExc_OverflowError,
928 "bytearray object is too large to make repr");
929 return NULL;
930 }
931 v = PyUnicode_FromUnicode(NULL, newsize);
932 if (v == NULL) {
933 return NULL;
934 }
935 else {
936 register Py_ssize_t i;
937 register Py_UNICODE c;
938 register Py_UNICODE *p;
939 int quote;
940
941 /* Figure out which quote to use; single is preferred */
942 quote = '\'';
943 {
944 char *test, *start;
945 start = PyByteArray_AS_STRING(self);
946 for (test = start; test < start+length; ++test) {
947 if (*test == '"') {
948 quote = '\''; /* back to single */
949 goto decided;
950 }
951 else if (*test == '\'')
952 quote = '"';
953 }
954 decided:
955 ;
956 }
957
958 p = PyUnicode_AS_UNICODE(v);
959 while (*quote_prefix)
960 *p++ = *quote_prefix++;
961 *p++ = quote;
962
963 for (i = 0; i < length; i++) {
964 /* There's at least enough room for a hex escape
965 and a closing quote. */
966 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
967 c = self->ob_bytes[i];
968 if (c == '\'' || c == '\\')
969 *p++ = '\\', *p++ = c;
970 else if (c == '\t')
971 *p++ = '\\', *p++ = 't';
972 else if (c == '\n')
973 *p++ = '\\', *p++ = 'n';
974 else if (c == '\r')
975 *p++ = '\\', *p++ = 'r';
976 else if (c == 0)
977 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
978 else if (c < ' ' || c >= 0x7f) {
979 *p++ = '\\';
980 *p++ = 'x';
981 *p++ = hexdigits[(c & 0xf0) >> 4];
982 *p++ = hexdigits[c & 0xf];
983 }
984 else
985 *p++ = c;
986 }
987 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
988 *p++ = quote;
989 while (*quote_postfix) {
990 *p++ = *quote_postfix++;
991 }
992 *p = '\0';
993 if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
994 Py_DECREF(v);
995 return NULL;
996 }
997 return v;
998 }
999}
1000
1001static PyObject *
1002bytes_str(PyObject *op)
1003{
1004#if 0
1005 if (Py_BytesWarningFlag) {
1006 if (PyErr_WarnEx(PyExc_BytesWarning,
1007 "str() on a bytearray instance", 1))
1008 return NULL;
1009 }
1010 return bytes_repr((PyByteArrayObject*)op);
1011#endif
1012 return PyBytes_FromStringAndSize(((PyByteArrayObject*)op)->ob_bytes, Py_SIZE(op));
1013}
1014
1015static PyObject *
1016bytes_richcompare(PyObject *self, PyObject *other, int op)
1017{
1018 Py_ssize_t self_size, other_size;
1019 Py_buffer self_bytes, other_bytes;
1020 PyObject *res;
1021 Py_ssize_t minsize;
1022 int cmp;
1023
1024 /* Bytes can be compared to anything that supports the (binary)
1025 buffer API. Except that a comparison with Unicode is always an
1026 error, even if the comparison is for equality. */
1027 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
1028 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
1029 if (Py_BytesWarningFlag && op == Py_EQ) {
1030 if (PyErr_WarnEx(PyExc_BytesWarning,
Ezio Melotti262c3ce2010-01-14 11:39:50 +00001031 "Comparison between bytearray and string", 1))
Christian Heimes44720832008-05-26 13:01:01 +00001032 return NULL;
1033 }
1034
1035 Py_INCREF(Py_NotImplemented);
1036 return Py_NotImplemented;
1037 }
1038
1039 self_size = _getbuffer(self, &self_bytes);
1040 if (self_size < 0) {
1041 PyErr_Clear();
1042 Py_INCREF(Py_NotImplemented);
1043 return Py_NotImplemented;
1044 }
1045
1046 other_size = _getbuffer(other, &other_bytes);
1047 if (other_size < 0) {
1048 PyErr_Clear();
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001049 PyBuffer_Release(&self_bytes);
Christian Heimes44720832008-05-26 13:01:01 +00001050 Py_INCREF(Py_NotImplemented);
1051 return Py_NotImplemented;
1052 }
1053
1054 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1055 /* Shortcut: if the lengths differ, the objects differ */
1056 cmp = (op == Py_NE);
1057 }
1058 else {
1059 minsize = self_size;
1060 if (other_size < minsize)
1061 minsize = other_size;
1062
1063 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1064 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1065
1066 if (cmp == 0) {
1067 if (self_size < other_size)
1068 cmp = -1;
1069 else if (self_size > other_size)
1070 cmp = 1;
1071 }
1072
1073 switch (op) {
1074 case Py_LT: cmp = cmp < 0; break;
1075 case Py_LE: cmp = cmp <= 0; break;
1076 case Py_EQ: cmp = cmp == 0; break;
1077 case Py_NE: cmp = cmp != 0; break;
1078 case Py_GT: cmp = cmp > 0; break;
1079 case Py_GE: cmp = cmp >= 0; break;
1080 }
1081 }
1082
1083 res = cmp ? Py_True : Py_False;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001084 PyBuffer_Release(&self_bytes);
1085 PyBuffer_Release(&other_bytes);
Christian Heimes44720832008-05-26 13:01:01 +00001086 Py_INCREF(res);
1087 return res;
1088}
1089
1090static void
1091bytes_dealloc(PyByteArrayObject *self)
1092{
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001093 if (self->ob_exports > 0) {
1094 PyErr_SetString(PyExc_SystemError,
Georg Brandle9b91212009-04-05 21:26:31 +00001095 "deallocated bytearray object has exported buffers");
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001096 PyErr_Print();
1097 }
Christian Heimes44720832008-05-26 13:01:01 +00001098 if (self->ob_bytes != 0) {
1099 PyMem_Free(self->ob_bytes);
1100 }
1101 Py_TYPE(self)->tp_free((PyObject *)self);
1102}
1103
1104
1105/* -------------------------------------------------------------------- */
1106/* Methods */
1107
1108#define STRINGLIB_CHAR char
1109#define STRINGLIB_CMP memcmp
1110#define STRINGLIB_LEN PyByteArray_GET_SIZE
1111#define STRINGLIB_STR PyByteArray_AS_STRING
1112#define STRINGLIB_NEW PyByteArray_FromStringAndSize
1113#define STRINGLIB_EMPTY nullbytes
1114#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1115#define STRINGLIB_MUTABLE 1
Christian Heimes7d4c3172008-08-22 19:47:25 +00001116#define FROM_BYTEARRAY 1
Christian Heimes44720832008-05-26 13:01:01 +00001117
1118#include "stringlib/fastsearch.h"
1119#include "stringlib/count.h"
1120#include "stringlib/find.h"
1121#include "stringlib/partition.h"
1122#include "stringlib/ctype.h"
1123#include "stringlib/transmogrify.h"
1124
1125
1126/* The following Py_LOCAL_INLINE and Py_LOCAL functions
1127were copied from the old char* style string object. */
1128
1129Py_LOCAL_INLINE(void)
1130_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
1131{
1132 if (*end > len)
1133 *end = len;
1134 else if (*end < 0)
1135 *end += len;
1136 if (*end < 0)
1137 *end = 0;
1138 if (*start < 0)
1139 *start += len;
1140 if (*start < 0)
1141 *start = 0;
1142}
1143
1144
1145Py_LOCAL_INLINE(Py_ssize_t)
1146bytes_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
1147{
1148 PyObject *subobj;
1149 Py_buffer subbuf;
1150 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1151 Py_ssize_t res;
1152
1153 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj,
1154 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1155 return -2;
1156 if (_getbuffer(subobj, &subbuf) < 0)
1157 return -2;
1158 if (dir > 0)
1159 res = stringlib_find_slice(
1160 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1161 subbuf.buf, subbuf.len, start, end);
1162 else
1163 res = stringlib_rfind_slice(
1164 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1165 subbuf.buf, subbuf.len, start, end);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001166 PyBuffer_Release(&subbuf);
Christian Heimes44720832008-05-26 13:01:01 +00001167 return res;
1168}
1169
1170PyDoc_STRVAR(find__doc__,
1171"B.find(sub [,start [,end]]) -> int\n\
1172\n\
1173Return the lowest index in B where subsection sub is found,\n\
1174such that sub is contained within s[start,end]. Optional\n\
1175arguments start and end are interpreted as in slice notation.\n\
1176\n\
1177Return -1 on failure.");
1178
1179static PyObject *
1180bytes_find(PyByteArrayObject *self, PyObject *args)
1181{
1182 Py_ssize_t result = bytes_find_internal(self, args, +1);
1183 if (result == -2)
1184 return NULL;
1185 return PyInt_FromSsize_t(result);
1186}
1187
1188PyDoc_STRVAR(count__doc__,
1189"B.count(sub [,start [,end]]) -> int\n\
1190\n\
1191Return the number of non-overlapping occurrences of subsection sub in\n\
1192bytes B[start:end]. Optional arguments start and end are interpreted\n\
1193as in slice notation.");
1194
1195static PyObject *
1196bytes_count(PyByteArrayObject *self, PyObject *args)
1197{
1198 PyObject *sub_obj;
1199 const char *str = PyByteArray_AS_STRING(self);
1200 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
1201 Py_buffer vsub;
1202 PyObject *count_obj;
1203
1204 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
1205 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1206 return NULL;
1207
1208 if (_getbuffer(sub_obj, &vsub) < 0)
1209 return NULL;
1210
1211 _adjust_indices(&start, &end, PyByteArray_GET_SIZE(self));
1212
1213 count_obj = PyInt_FromSsize_t(
1214 stringlib_count(str + start, end - start, vsub.buf, vsub.len)
1215 );
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001216 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00001217 return count_obj;
1218}
1219
1220
1221PyDoc_STRVAR(index__doc__,
1222"B.index(sub [,start [,end]]) -> int\n\
1223\n\
1224Like B.find() but raise ValueError when the subsection is not found.");
1225
1226static PyObject *
1227bytes_index(PyByteArrayObject *self, PyObject *args)
1228{
1229 Py_ssize_t result = bytes_find_internal(self, args, +1);
1230 if (result == -2)
1231 return NULL;
1232 if (result == -1) {
1233 PyErr_SetString(PyExc_ValueError,
1234 "subsection not found");
1235 return NULL;
1236 }
1237 return PyInt_FromSsize_t(result);
1238}
1239
1240
1241PyDoc_STRVAR(rfind__doc__,
1242"B.rfind(sub [,start [,end]]) -> int\n\
1243\n\
1244Return the highest index in B where subsection sub is found,\n\
1245such that sub is contained within s[start,end]. Optional\n\
1246arguments start and end are interpreted as in slice notation.\n\
1247\n\
1248Return -1 on failure.");
1249
1250static PyObject *
1251bytes_rfind(PyByteArrayObject *self, PyObject *args)
1252{
1253 Py_ssize_t result = bytes_find_internal(self, args, -1);
1254 if (result == -2)
1255 return NULL;
1256 return PyInt_FromSsize_t(result);
1257}
1258
1259
1260PyDoc_STRVAR(rindex__doc__,
1261"B.rindex(sub [,start [,end]]) -> int\n\
1262\n\
1263Like B.rfind() but raise ValueError when the subsection is not found.");
1264
1265static PyObject *
1266bytes_rindex(PyByteArrayObject *self, PyObject *args)
1267{
1268 Py_ssize_t result = bytes_find_internal(self, args, -1);
1269 if (result == -2)
1270 return NULL;
1271 if (result == -1) {
1272 PyErr_SetString(PyExc_ValueError,
1273 "subsection not found");
1274 return NULL;
1275 }
1276 return PyInt_FromSsize_t(result);
1277}
1278
1279
1280static int
1281bytes_contains(PyObject *self, PyObject *arg)
1282{
1283 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1284 if (ival == -1 && PyErr_Occurred()) {
1285 Py_buffer varg;
1286 int pos;
1287 PyErr_Clear();
1288 if (_getbuffer(arg, &varg) < 0)
1289 return -1;
1290 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1291 varg.buf, varg.len, 0);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001292 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00001293 return pos >= 0;
1294 }
1295 if (ival < 0 || ival >= 256) {
1296 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1297 return -1;
1298 }
1299
1300 return memchr(PyByteArray_AS_STRING(self), ival, Py_SIZE(self)) != NULL;
1301}
1302
1303
1304/* Matches the end (direction >= 0) or start (direction < 0) of self
1305 * against substr, using the start and end arguments. Returns
1306 * -1 on error, 0 if not found and 1 if found.
1307 */
1308Py_LOCAL(int)
1309_bytes_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
1310 Py_ssize_t end, int direction)
1311{
1312 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1313 const char* str;
1314 Py_buffer vsubstr;
1315 int rv = 0;
1316
1317 str = PyByteArray_AS_STRING(self);
1318
1319 if (_getbuffer(substr, &vsubstr) < 0)
1320 return -1;
1321
1322 _adjust_indices(&start, &end, len);
1323
1324 if (direction < 0) {
1325 /* startswith */
1326 if (start+vsubstr.len > len) {
1327 goto done;
1328 }
1329 } else {
1330 /* endswith */
1331 if (end-start < vsubstr.len || start > len) {
1332 goto done;
1333 }
1334
1335 if (end-vsubstr.len > start)
1336 start = end - vsubstr.len;
1337 }
1338 if (end-start >= vsubstr.len)
1339 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1340
1341done:
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001342 PyBuffer_Release(&vsubstr);
Christian Heimes44720832008-05-26 13:01:01 +00001343 return rv;
1344}
1345
1346
1347PyDoc_STRVAR(startswith__doc__,
1348"B.startswith(prefix [,start [,end]]) -> bool\n\
1349\n\
1350Return True if B starts with the specified prefix, False otherwise.\n\
1351With optional start, test B beginning at that position.\n\
1352With optional end, stop comparing B at that position.\n\
1353prefix can also be a tuple of strings to try.");
1354
1355static PyObject *
1356bytes_startswith(PyByteArrayObject *self, PyObject *args)
1357{
1358 Py_ssize_t start = 0;
1359 Py_ssize_t end = PY_SSIZE_T_MAX;
1360 PyObject *subobj;
1361 int result;
1362
1363 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
1364 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1365 return NULL;
1366 if (PyTuple_Check(subobj)) {
1367 Py_ssize_t i;
1368 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1369 result = _bytes_tailmatch(self,
1370 PyTuple_GET_ITEM(subobj, i),
1371 start, end, -1);
1372 if (result == -1)
1373 return NULL;
1374 else if (result) {
1375 Py_RETURN_TRUE;
1376 }
1377 }
1378 Py_RETURN_FALSE;
1379 }
1380 result = _bytes_tailmatch(self, subobj, start, end, -1);
1381 if (result == -1)
1382 return NULL;
1383 else
1384 return PyBool_FromLong(result);
1385}
1386
1387PyDoc_STRVAR(endswith__doc__,
1388"B.endswith(suffix [,start [,end]]) -> bool\n\
1389\n\
1390Return True if B ends with the specified suffix, False otherwise.\n\
1391With optional start, test B beginning at that position.\n\
1392With optional end, stop comparing B at that position.\n\
1393suffix can also be a tuple of strings to try.");
1394
1395static PyObject *
1396bytes_endswith(PyByteArrayObject *self, PyObject *args)
1397{
1398 Py_ssize_t start = 0;
1399 Py_ssize_t end = PY_SSIZE_T_MAX;
1400 PyObject *subobj;
1401 int result;
1402
1403 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
1404 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1405 return NULL;
1406 if (PyTuple_Check(subobj)) {
1407 Py_ssize_t i;
1408 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1409 result = _bytes_tailmatch(self,
1410 PyTuple_GET_ITEM(subobj, i),
1411 start, end, +1);
1412 if (result == -1)
1413 return NULL;
1414 else if (result) {
1415 Py_RETURN_TRUE;
1416 }
1417 }
1418 Py_RETURN_FALSE;
1419 }
1420 result = _bytes_tailmatch(self, subobj, start, end, +1);
1421 if (result == -1)
1422 return NULL;
1423 else
1424 return PyBool_FromLong(result);
1425}
1426
1427
1428PyDoc_STRVAR(translate__doc__,
1429"B.translate(table[, deletechars]) -> bytearray\n\
1430\n\
1431Return a copy of B, where all characters occurring in the\n\
1432optional argument deletechars are removed, and the remaining\n\
1433characters have been mapped through the given translation\n\
1434table, which must be a bytes object of length 256.");
1435
1436static PyObject *
1437bytes_translate(PyByteArrayObject *self, PyObject *args)
1438{
1439 register char *input, *output;
1440 register const char *table;
Benjamin Peterson866eba92008-11-19 22:05:53 +00001441 register Py_ssize_t i, c;
Christian Heimes44720832008-05-26 13:01:01 +00001442 PyObject *input_obj = (PyObject*)self;
1443 const char *output_start;
1444 Py_ssize_t inlen;
1445 PyObject *result;
1446 int trans_table[256];
1447 PyObject *tableobj, *delobj = NULL;
1448 Py_buffer vtable, vdel;
1449
1450 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
1451 &tableobj, &delobj))
1452 return NULL;
1453
1454 if (_getbuffer(tableobj, &vtable) < 0)
1455 return NULL;
1456
1457 if (vtable.len != 256) {
1458 PyErr_SetString(PyExc_ValueError,
1459 "translation table must be 256 characters long");
Georg Brandl11a81b22009-07-22 12:03:09 +00001460 PyBuffer_Release(&vtable);
1461 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00001462 }
1463
1464 if (delobj != NULL) {
1465 if (_getbuffer(delobj, &vdel) < 0) {
Georg Brandl11a81b22009-07-22 12:03:09 +00001466 PyBuffer_Release(&vtable);
1467 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00001468 }
1469 }
1470 else {
1471 vdel.buf = NULL;
1472 vdel.len = 0;
1473 }
1474
1475 table = (const char *)vtable.buf;
1476 inlen = PyByteArray_GET_SIZE(input_obj);
1477 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1478 if (result == NULL)
1479 goto done;
1480 output_start = output = PyByteArray_AsString(result);
1481 input = PyByteArray_AS_STRING(input_obj);
1482
1483 if (vdel.len == 0) {
1484 /* If no deletions are required, use faster code */
1485 for (i = inlen; --i >= 0; ) {
1486 c = Py_CHARMASK(*input++);
Benjamin Peterson866eba92008-11-19 22:05:53 +00001487 *output++ = table[c];
Christian Heimes44720832008-05-26 13:01:01 +00001488 }
Christian Heimes44720832008-05-26 13:01:01 +00001489 goto done;
1490 }
Antoine Pitrou599db7f2008-12-07 00:07:51 +00001491
Christian Heimes44720832008-05-26 13:01:01 +00001492 for (i = 0; i < 256; i++)
1493 trans_table[i] = Py_CHARMASK(table[i]);
1494
1495 for (i = 0; i < vdel.len; i++)
1496 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1497
1498 for (i = inlen; --i >= 0; ) {
1499 c = Py_CHARMASK(*input++);
1500 if (trans_table[c] != -1)
1501 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1502 continue;
Christian Heimes44720832008-05-26 13:01:01 +00001503 }
1504 /* Fix the size of the resulting string */
1505 if (inlen > 0)
1506 PyByteArray_Resize(result, output - output_start);
1507
1508done:
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001509 PyBuffer_Release(&vtable);
Christian Heimes44720832008-05-26 13:01:01 +00001510 if (delobj != NULL)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001511 PyBuffer_Release(&vdel);
Christian Heimes44720832008-05-26 13:01:01 +00001512 return result;
1513}
1514
1515
1516#define FORWARD 1
1517#define REVERSE -1
1518
1519/* find and count characters and substrings */
1520
1521#define findchar(target, target_len, c) \
1522 ((char *)memchr((const void *)(target), c, target_len))
1523
1524/* Don't call if length < 2 */
1525#define Py_STRING_MATCH(target, offset, pattern, length) \
1526 (target[offset] == pattern[0] && \
1527 target[offset+length-1] == pattern[length-1] && \
1528 !memcmp(target+offset+1, pattern+1, length-2) )
1529
1530
Benjamin Peterson866eba92008-11-19 22:05:53 +00001531/* Bytes ops must return a string, create a copy */
Christian Heimes44720832008-05-26 13:01:01 +00001532Py_LOCAL(PyByteArrayObject *)
1533return_self(PyByteArrayObject *self)
1534{
Christian Heimes44720832008-05-26 13:01:01 +00001535 return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1536 PyByteArray_AS_STRING(self),
1537 PyByteArray_GET_SIZE(self));
1538}
1539
1540Py_LOCAL_INLINE(Py_ssize_t)
1541countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1542{
1543 Py_ssize_t count=0;
1544 const char *start=target;
1545 const char *end=target+target_len;
1546
1547 while ( (start=findchar(start, end-start, c)) != NULL ) {
1548 count++;
1549 if (count >= maxcount)
1550 break;
1551 start += 1;
1552 }
1553 return count;
1554}
1555
1556Py_LOCAL(Py_ssize_t)
1557findstring(const char *target, Py_ssize_t target_len,
1558 const char *pattern, Py_ssize_t pattern_len,
1559 Py_ssize_t start,
1560 Py_ssize_t end,
1561 int direction)
1562{
1563 if (start < 0) {
1564 start += target_len;
1565 if (start < 0)
1566 start = 0;
1567 }
1568 if (end > target_len) {
1569 end = target_len;
1570 } else if (end < 0) {
1571 end += target_len;
1572 if (end < 0)
1573 end = 0;
1574 }
1575
1576 /* zero-length substrings always match at the first attempt */
1577 if (pattern_len == 0)
1578 return (direction > 0) ? start : end;
1579
1580 end -= pattern_len;
1581
1582 if (direction < 0) {
1583 for (; end >= start; end--)
1584 if (Py_STRING_MATCH(target, end, pattern, pattern_len))
1585 return end;
1586 } else {
1587 for (; start <= end; start++)
1588 if (Py_STRING_MATCH(target, start, pattern, pattern_len))
1589 return start;
1590 }
1591 return -1;
1592}
1593
1594Py_LOCAL_INLINE(Py_ssize_t)
1595countstring(const char *target, Py_ssize_t target_len,
1596 const char *pattern, Py_ssize_t pattern_len,
1597 Py_ssize_t start,
1598 Py_ssize_t end,
1599 int direction, Py_ssize_t maxcount)
1600{
1601 Py_ssize_t count=0;
1602
1603 if (start < 0) {
1604 start += target_len;
1605 if (start < 0)
1606 start = 0;
1607 }
1608 if (end > target_len) {
1609 end = target_len;
1610 } else if (end < 0) {
1611 end += target_len;
1612 if (end < 0)
1613 end = 0;
1614 }
1615
1616 /* zero-length substrings match everywhere */
1617 if (pattern_len == 0 || maxcount == 0) {
1618 if (target_len+1 < maxcount)
1619 return target_len+1;
1620 return maxcount;
1621 }
1622
1623 end -= pattern_len;
1624 if (direction < 0) {
1625 for (; (end >= start); end--)
1626 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) {
1627 count++;
1628 if (--maxcount <= 0) break;
1629 end -= pattern_len-1;
1630 }
1631 } else {
1632 for (; (start <= end); start++)
1633 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) {
1634 count++;
1635 if (--maxcount <= 0)
1636 break;
1637 start += pattern_len-1;
1638 }
1639 }
1640 return count;
1641}
1642
1643
1644/* Algorithms for different cases of string replacement */
1645
1646/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1647Py_LOCAL(PyByteArrayObject *)
1648replace_interleave(PyByteArrayObject *self,
1649 const char *to_s, Py_ssize_t to_len,
1650 Py_ssize_t maxcount)
1651{
1652 char *self_s, *result_s;
1653 Py_ssize_t self_len, result_len;
1654 Py_ssize_t count, i, product;
1655 PyByteArrayObject *result;
1656
1657 self_len = PyByteArray_GET_SIZE(self);
1658
1659 /* 1 at the end plus 1 after every character */
1660 count = self_len+1;
1661 if (maxcount < count)
1662 count = maxcount;
1663
1664 /* Check for overflow */
1665 /* result_len = count * to_len + self_len; */
1666 product = count * to_len;
1667 if (product / to_len != count) {
1668 PyErr_SetString(PyExc_OverflowError,
1669 "replace string is too long");
1670 return NULL;
1671 }
1672 result_len = product + self_len;
1673 if (result_len < 0) {
1674 PyErr_SetString(PyExc_OverflowError,
1675 "replace string is too long");
1676 return NULL;
1677 }
1678
1679 if (! (result = (PyByteArrayObject *)
1680 PyByteArray_FromStringAndSize(NULL, result_len)) )
1681 return NULL;
1682
1683 self_s = PyByteArray_AS_STRING(self);
1684 result_s = PyByteArray_AS_STRING(result);
1685
1686 /* TODO: special case single character, which doesn't need memcpy */
1687
1688 /* Lay the first one down (guaranteed this will occur) */
1689 Py_MEMCPY(result_s, to_s, to_len);
1690 result_s += to_len;
1691 count -= 1;
1692
1693 for (i=0; i<count; i++) {
1694 *result_s++ = *self_s++;
1695 Py_MEMCPY(result_s, to_s, to_len);
1696 result_s += to_len;
1697 }
1698
1699 /* Copy the rest of the original string */
1700 Py_MEMCPY(result_s, self_s, self_len-i);
1701
1702 return result;
1703}
1704
1705/* Special case for deleting a single character */
1706/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1707Py_LOCAL(PyByteArrayObject *)
1708replace_delete_single_character(PyByteArrayObject *self,
1709 char from_c, Py_ssize_t maxcount)
1710{
1711 char *self_s, *result_s;
1712 char *start, *next, *end;
1713 Py_ssize_t self_len, result_len;
1714 Py_ssize_t count;
1715 PyByteArrayObject *result;
1716
1717 self_len = PyByteArray_GET_SIZE(self);
1718 self_s = PyByteArray_AS_STRING(self);
1719
1720 count = countchar(self_s, self_len, from_c, maxcount);
1721 if (count == 0) {
1722 return return_self(self);
1723 }
1724
1725 result_len = self_len - count; /* from_len == 1 */
1726 assert(result_len>=0);
1727
1728 if ( (result = (PyByteArrayObject *)
1729 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1730 return NULL;
1731 result_s = PyByteArray_AS_STRING(result);
1732
1733 start = self_s;
1734 end = self_s + self_len;
1735 while (count-- > 0) {
1736 next = findchar(start, end-start, from_c);
1737 if (next == NULL)
1738 break;
1739 Py_MEMCPY(result_s, start, next-start);
1740 result_s += (next-start);
1741 start = next+1;
1742 }
1743 Py_MEMCPY(result_s, start, end-start);
1744
1745 return result;
1746}
1747
1748/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1749
1750Py_LOCAL(PyByteArrayObject *)
1751replace_delete_substring(PyByteArrayObject *self,
1752 const char *from_s, Py_ssize_t from_len,
1753 Py_ssize_t maxcount)
1754{
1755 char *self_s, *result_s;
1756 char *start, *next, *end;
1757 Py_ssize_t self_len, result_len;
1758 Py_ssize_t count, offset;
1759 PyByteArrayObject *result;
1760
1761 self_len = PyByteArray_GET_SIZE(self);
1762 self_s = PyByteArray_AS_STRING(self);
1763
1764 count = countstring(self_s, self_len,
1765 from_s, from_len,
1766 0, self_len, 1,
1767 maxcount);
1768
1769 if (count == 0) {
1770 /* no matches */
1771 return return_self(self);
1772 }
1773
1774 result_len = self_len - (count * from_len);
1775 assert (result_len>=0);
1776
1777 if ( (result = (PyByteArrayObject *)
1778 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL )
1779 return NULL;
1780
1781 result_s = PyByteArray_AS_STRING(result);
1782
1783 start = self_s;
1784 end = self_s + self_len;
1785 while (count-- > 0) {
1786 offset = findstring(start, end-start,
1787 from_s, from_len,
1788 0, end-start, FORWARD);
1789 if (offset == -1)
1790 break;
1791 next = start + offset;
1792
1793 Py_MEMCPY(result_s, start, next-start);
1794
1795 result_s += (next-start);
1796 start = next+from_len;
1797 }
1798 Py_MEMCPY(result_s, start, end-start);
1799 return result;
1800}
1801
1802/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1803Py_LOCAL(PyByteArrayObject *)
1804replace_single_character_in_place(PyByteArrayObject *self,
1805 char from_c, char to_c,
1806 Py_ssize_t maxcount)
1807{
1808 char *self_s, *result_s, *start, *end, *next;
1809 Py_ssize_t self_len;
1810 PyByteArrayObject *result;
1811
1812 /* The result string will be the same size */
1813 self_s = PyByteArray_AS_STRING(self);
1814 self_len = PyByteArray_GET_SIZE(self);
1815
1816 next = findchar(self_s, self_len, from_c);
1817
1818 if (next == NULL) {
1819 /* No matches; return the original bytes */
1820 return return_self(self);
1821 }
1822
1823 /* Need to make a new bytes */
1824 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1825 if (result == NULL)
1826 return NULL;
1827 result_s = PyByteArray_AS_STRING(result);
1828 Py_MEMCPY(result_s, self_s, self_len);
1829
1830 /* change everything in-place, starting with this one */
1831 start = result_s + (next-self_s);
1832 *start = to_c;
1833 start++;
1834 end = result_s + self_len;
1835
1836 while (--maxcount > 0) {
1837 next = findchar(start, end-start, from_c);
1838 if (next == NULL)
1839 break;
1840 *next = to_c;
1841 start = next+1;
1842 }
1843
1844 return result;
1845}
1846
1847/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1848Py_LOCAL(PyByteArrayObject *)
1849replace_substring_in_place(PyByteArrayObject *self,
1850 const char *from_s, Py_ssize_t from_len,
1851 const char *to_s, Py_ssize_t to_len,
1852 Py_ssize_t maxcount)
1853{
1854 char *result_s, *start, *end;
1855 char *self_s;
1856 Py_ssize_t self_len, offset;
1857 PyByteArrayObject *result;
1858
1859 /* The result bytes will be the same size */
1860
1861 self_s = PyByteArray_AS_STRING(self);
1862 self_len = PyByteArray_GET_SIZE(self);
1863
1864 offset = findstring(self_s, self_len,
1865 from_s, from_len,
1866 0, self_len, FORWARD);
1867 if (offset == -1) {
1868 /* No matches; return the original bytes */
1869 return return_self(self);
1870 }
1871
1872 /* Need to make a new bytes */
1873 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1874 if (result == NULL)
1875 return NULL;
1876 result_s = PyByteArray_AS_STRING(result);
1877 Py_MEMCPY(result_s, self_s, self_len);
1878
1879 /* change everything in-place, starting with this one */
1880 start = result_s + offset;
1881 Py_MEMCPY(start, to_s, from_len);
1882 start += from_len;
1883 end = result_s + self_len;
1884
1885 while ( --maxcount > 0) {
1886 offset = findstring(start, end-start,
1887 from_s, from_len,
1888 0, end-start, FORWARD);
1889 if (offset==-1)
1890 break;
1891 Py_MEMCPY(start+offset, to_s, from_len);
1892 start += offset+from_len;
1893 }
1894
1895 return result;
1896}
1897
1898/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
1899Py_LOCAL(PyByteArrayObject *)
1900replace_single_character(PyByteArrayObject *self,
1901 char from_c,
1902 const char *to_s, Py_ssize_t to_len,
1903 Py_ssize_t maxcount)
1904{
1905 char *self_s, *result_s;
1906 char *start, *next, *end;
1907 Py_ssize_t self_len, result_len;
1908 Py_ssize_t count, product;
1909 PyByteArrayObject *result;
1910
1911 self_s = PyByteArray_AS_STRING(self);
1912 self_len = PyByteArray_GET_SIZE(self);
1913
1914 count = countchar(self_s, self_len, from_c, maxcount);
1915 if (count == 0) {
1916 /* no matches, return unchanged */
1917 return return_self(self);
1918 }
1919
1920 /* use the difference between current and new, hence the "-1" */
1921 /* result_len = self_len + count * (to_len-1) */
1922 product = count * (to_len-1);
1923 if (product / (to_len-1) != count) {
1924 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1925 return NULL;
1926 }
1927 result_len = self_len + product;
1928 if (result_len < 0) {
1929 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1930 return NULL;
1931 }
1932
1933 if ( (result = (PyByteArrayObject *)
1934 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1935 return NULL;
1936 result_s = PyByteArray_AS_STRING(result);
1937
1938 start = self_s;
1939 end = self_s + self_len;
1940 while (count-- > 0) {
1941 next = findchar(start, end-start, from_c);
1942 if (next == NULL)
1943 break;
1944
1945 if (next == start) {
1946 /* replace with the 'to' */
1947 Py_MEMCPY(result_s, to_s, to_len);
1948 result_s += to_len;
1949 start += 1;
1950 } else {
1951 /* copy the unchanged old then the 'to' */
1952 Py_MEMCPY(result_s, start, next-start);
1953 result_s += (next-start);
1954 Py_MEMCPY(result_s, to_s, to_len);
1955 result_s += to_len;
1956 start = next+1;
1957 }
1958 }
1959 /* Copy the remainder of the remaining bytes */
1960 Py_MEMCPY(result_s, start, end-start);
1961
1962 return result;
1963}
1964
1965/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
1966Py_LOCAL(PyByteArrayObject *)
1967replace_substring(PyByteArrayObject *self,
1968 const char *from_s, Py_ssize_t from_len,
1969 const char *to_s, Py_ssize_t to_len,
1970 Py_ssize_t maxcount)
1971{
1972 char *self_s, *result_s;
1973 char *start, *next, *end;
1974 Py_ssize_t self_len, result_len;
1975 Py_ssize_t count, offset, product;
1976 PyByteArrayObject *result;
1977
1978 self_s = PyByteArray_AS_STRING(self);
1979 self_len = PyByteArray_GET_SIZE(self);
1980
1981 count = countstring(self_s, self_len,
1982 from_s, from_len,
1983 0, self_len, FORWARD, maxcount);
1984 if (count == 0) {
1985 /* no matches, return unchanged */
1986 return return_self(self);
1987 }
1988
1989 /* Check for overflow */
1990 /* result_len = self_len + count * (to_len-from_len) */
1991 product = count * (to_len-from_len);
1992 if (product / (to_len-from_len) != count) {
1993 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1994 return NULL;
1995 }
1996 result_len = self_len + product;
1997 if (result_len < 0) {
1998 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1999 return NULL;
2000 }
2001
2002 if ( (result = (PyByteArrayObject *)
2003 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
2004 return NULL;
2005 result_s = PyByteArray_AS_STRING(result);
2006
2007 start = self_s;
2008 end = self_s + self_len;
2009 while (count-- > 0) {
2010 offset = findstring(start, end-start,
2011 from_s, from_len,
2012 0, end-start, FORWARD);
2013 if (offset == -1)
2014 break;
2015 next = start+offset;
2016 if (next == start) {
2017 /* replace with the 'to' */
2018 Py_MEMCPY(result_s, to_s, to_len);
2019 result_s += to_len;
2020 start += from_len;
2021 } else {
2022 /* copy the unchanged old then the 'to' */
2023 Py_MEMCPY(result_s, start, next-start);
2024 result_s += (next-start);
2025 Py_MEMCPY(result_s, to_s, to_len);
2026 result_s += to_len;
2027 start = next+from_len;
2028 }
2029 }
2030 /* Copy the remainder of the remaining bytes */
2031 Py_MEMCPY(result_s, start, end-start);
2032
2033 return result;
2034}
2035
2036
2037Py_LOCAL(PyByteArrayObject *)
2038replace(PyByteArrayObject *self,
2039 const char *from_s, Py_ssize_t from_len,
2040 const char *to_s, Py_ssize_t to_len,
2041 Py_ssize_t maxcount)
2042{
2043 if (maxcount < 0) {
2044 maxcount = PY_SSIZE_T_MAX;
2045 } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) {
2046 /* nothing to do; return the original bytes */
2047 return return_self(self);
2048 }
2049
2050 if (maxcount == 0 ||
2051 (from_len == 0 && to_len == 0)) {
2052 /* nothing to do; return the original bytes */
2053 return return_self(self);
2054 }
2055
2056 /* Handle zero-length special cases */
2057
2058 if (from_len == 0) {
2059 /* insert the 'to' bytes everywhere. */
2060 /* >>> "Python".replace("", ".") */
2061 /* '.P.y.t.h.o.n.' */
2062 return replace_interleave(self, to_s, to_len, maxcount);
2063 }
2064
2065 /* Except for "".replace("", "A") == "A" there is no way beyond this */
2066 /* point for an empty self bytes to generate a non-empty bytes */
2067 /* Special case so the remaining code always gets a non-empty bytes */
2068 if (PyByteArray_GET_SIZE(self) == 0) {
2069 return return_self(self);
2070 }
2071
2072 if (to_len == 0) {
2073 /* delete all occurances of 'from' bytes */
2074 if (from_len == 1) {
2075 return replace_delete_single_character(
2076 self, from_s[0], maxcount);
2077 } else {
2078 return replace_delete_substring(self, from_s, from_len, maxcount);
2079 }
2080 }
2081
2082 /* Handle special case where both bytes have the same length */
2083
2084 if (from_len == to_len) {
2085 if (from_len == 1) {
2086 return replace_single_character_in_place(
2087 self,
2088 from_s[0],
2089 to_s[0],
2090 maxcount);
2091 } else {
2092 return replace_substring_in_place(
2093 self, from_s, from_len, to_s, to_len, maxcount);
2094 }
2095 }
2096
2097 /* Otherwise use the more generic algorithms */
2098 if (from_len == 1) {
2099 return replace_single_character(self, from_s[0],
2100 to_s, to_len, maxcount);
2101 } else {
2102 /* len('from')>=2, len('to')>=1 */
2103 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
2104 }
2105}
2106
2107
2108PyDoc_STRVAR(replace__doc__,
2109"B.replace(old, new[, count]) -> bytes\n\
2110\n\
2111Return a copy of B with all occurrences of subsection\n\
2112old replaced by new. If the optional argument count is\n\
2113given, only the first count occurrences are replaced.");
2114
2115static PyObject *
2116bytes_replace(PyByteArrayObject *self, PyObject *args)
2117{
2118 Py_ssize_t count = -1;
2119 PyObject *from, *to, *res;
2120 Py_buffer vfrom, vto;
2121
2122 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
2123 return NULL;
2124
2125 if (_getbuffer(from, &vfrom) < 0)
2126 return NULL;
2127 if (_getbuffer(to, &vto) < 0) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002128 PyBuffer_Release(&vfrom);
Christian Heimes44720832008-05-26 13:01:01 +00002129 return NULL;
2130 }
2131
2132 res = (PyObject *)replace((PyByteArrayObject *) self,
2133 vfrom.buf, vfrom.len,
2134 vto.buf, vto.len, count);
2135
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002136 PyBuffer_Release(&vfrom);
2137 PyBuffer_Release(&vto);
Christian Heimes44720832008-05-26 13:01:01 +00002138 return res;
2139}
2140
2141
2142/* Overallocate the initial list to reduce the number of reallocs for small
2143 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three
2144 resizes, to sizes 4, 8, then 16. Most observed string splits are for human
2145 text (roughly 11 words per line) and field delimited data (usually 1-10
2146 fields). For large strings the split algorithms are bandwidth limited
2147 so increasing the preallocation likely will not improve things.*/
2148
2149#define MAX_PREALLOC 12
2150
2151/* 5 splits gives 6 elements */
2152#define PREALLOC_SIZE(maxsplit) \
2153 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
2154
2155#define SPLIT_APPEND(data, left, right) \
2156 str = PyByteArray_FromStringAndSize((data) + (left), \
2157 (right) - (left)); \
2158 if (str == NULL) \
2159 goto onError; \
2160 if (PyList_Append(list, str)) { \
2161 Py_DECREF(str); \
2162 goto onError; \
2163 } \
2164 else \
2165 Py_DECREF(str);
2166
2167#define SPLIT_ADD(data, left, right) { \
2168 str = PyByteArray_FromStringAndSize((data) + (left), \
2169 (right) - (left)); \
2170 if (str == NULL) \
2171 goto onError; \
2172 if (count < MAX_PREALLOC) { \
2173 PyList_SET_ITEM(list, count, str); \
2174 } else { \
2175 if (PyList_Append(list, str)) { \
2176 Py_DECREF(str); \
2177 goto onError; \
2178 } \
2179 else \
2180 Py_DECREF(str); \
2181 } \
2182 count++; }
2183
2184/* Always force the list to the expected size. */
2185#define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count
2186
2187
2188Py_LOCAL_INLINE(PyObject *)
2189split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
2190{
2191 register Py_ssize_t i, j, count = 0;
2192 PyObject *str;
2193 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
2194
2195 if (list == NULL)
2196 return NULL;
2197
2198 i = j = 0;
2199 while ((j < len) && (maxcount-- > 0)) {
2200 for(; j < len; j++) {
2201 /* I found that using memchr makes no difference */
2202 if (s[j] == ch) {
2203 SPLIT_ADD(s, i, j);
2204 i = j = j + 1;
2205 break;
2206 }
2207 }
2208 }
2209 if (i <= len) {
2210 SPLIT_ADD(s, i, len);
2211 }
2212 FIX_PREALLOC_SIZE(list);
2213 return list;
2214
2215 onError:
2216 Py_DECREF(list);
2217 return NULL;
2218}
2219
2220
2221Py_LOCAL_INLINE(PyObject *)
2222split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount)
2223{
2224 register Py_ssize_t i, j, count = 0;
2225 PyObject *str;
2226 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
2227
2228 if (list == NULL)
2229 return NULL;
2230
2231 for (i = j = 0; i < len; ) {
2232 /* find a token */
2233 while (i < len && ISSPACE(s[i]))
2234 i++;
2235 j = i;
2236 while (i < len && !ISSPACE(s[i]))
2237 i++;
2238 if (j < i) {
2239 if (maxcount-- <= 0)
2240 break;
2241 SPLIT_ADD(s, j, i);
2242 while (i < len && ISSPACE(s[i]))
2243 i++;
2244 j = i;
2245 }
2246 }
2247 if (j < len) {
2248 SPLIT_ADD(s, j, len);
2249 }
2250 FIX_PREALLOC_SIZE(list);
2251 return list;
2252
2253 onError:
2254 Py_DECREF(list);
2255 return NULL;
2256}
2257
2258PyDoc_STRVAR(split__doc__,
2259"B.split([sep[, maxsplit]]) -> list of bytearray\n\
2260\n\
2261Return a list of the sections in B, using sep as the delimiter.\n\
2262If sep is not given, B is split on ASCII whitespace characters\n\
2263(space, tab, return, newline, formfeed, vertical tab).\n\
2264If maxsplit is given, at most maxsplit splits are done.");
2265
2266static PyObject *
2267bytes_split(PyByteArrayObject *self, PyObject *args)
2268{
2269 Py_ssize_t len = PyByteArray_GET_SIZE(self), n, i, j;
2270 Py_ssize_t maxsplit = -1, count = 0;
2271 const char *s = PyByteArray_AS_STRING(self), *sub;
2272 PyObject *list, *str, *subobj = Py_None;
2273 Py_buffer vsub;
2274#ifdef USE_FAST
2275 Py_ssize_t pos;
2276#endif
2277
2278 if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit))
2279 return NULL;
2280 if (maxsplit < 0)
2281 maxsplit = PY_SSIZE_T_MAX;
2282
2283 if (subobj == Py_None)
2284 return split_whitespace(s, len, maxsplit);
2285
2286 if (_getbuffer(subobj, &vsub) < 0)
2287 return NULL;
2288 sub = vsub.buf;
2289 n = vsub.len;
2290
2291 if (n == 0) {
2292 PyErr_SetString(PyExc_ValueError, "empty separator");
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002293 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002294 return NULL;
2295 }
Amaury Forgeot d'Arc313bda12008-08-17 21:05:18 +00002296 if (n == 1) {
2297 list = split_char(s, len, sub[0], maxsplit);
2298 PyBuffer_Release(&vsub);
2299 return list;
2300 }
Christian Heimes44720832008-05-26 13:01:01 +00002301
2302 list = PyList_New(PREALLOC_SIZE(maxsplit));
2303 if (list == NULL) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002304 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002305 return NULL;
2306 }
2307
2308#ifdef USE_FAST
2309 i = j = 0;
2310 while (maxsplit-- > 0) {
2311 pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH);
2312 if (pos < 0)
2313 break;
2314 j = i+pos;
2315 SPLIT_ADD(s, i, j);
2316 i = j + n;
2317 }
2318#else
2319 i = j = 0;
2320 while ((j+n <= len) && (maxsplit-- > 0)) {
2321 for (; j+n <= len; j++) {
2322 if (Py_STRING_MATCH(s, j, sub, n)) {
2323 SPLIT_ADD(s, i, j);
2324 i = j = j + n;
2325 break;
2326 }
2327 }
2328 }
2329#endif
2330 SPLIT_ADD(s, i, len);
2331 FIX_PREALLOC_SIZE(list);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002332 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002333 return list;
2334
2335 onError:
2336 Py_DECREF(list);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002337 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002338 return NULL;
2339}
2340
2341/* stringlib's partition shares nullbytes in some cases.
2342 undo this, we don't want the nullbytes to be shared. */
2343static PyObject *
2344make_nullbytes_unique(PyObject *result)
2345{
2346 if (result != NULL) {
2347 int i;
2348 assert(PyTuple_Check(result));
2349 assert(PyTuple_GET_SIZE(result) == 3);
2350 for (i = 0; i < 3; i++) {
2351 if (PyTuple_GET_ITEM(result, i) == (PyObject *)nullbytes) {
2352 PyObject *new = PyByteArray_FromStringAndSize(NULL, 0);
2353 if (new == NULL) {
2354 Py_DECREF(result);
2355 result = NULL;
2356 break;
2357 }
2358 Py_DECREF(nullbytes);
2359 PyTuple_SET_ITEM(result, i, new);
2360 }
2361 }
2362 }
2363 return result;
2364}
2365
2366PyDoc_STRVAR(partition__doc__,
2367"B.partition(sep) -> (head, sep, tail)\n\
2368\n\
2369Searches for the separator sep in B, and returns the part before it,\n\
2370the separator itself, and the part after it. If the separator is not\n\
2371found, returns B and two empty bytearray objects.");
2372
2373static PyObject *
2374bytes_partition(PyByteArrayObject *self, PyObject *sep_obj)
2375{
2376 PyObject *bytesep, *result;
2377
2378 bytesep = PyByteArray_FromObject(sep_obj);
2379 if (! bytesep)
2380 return NULL;
2381
2382 result = stringlib_partition(
2383 (PyObject*) self,
2384 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2385 bytesep,
2386 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2387 );
2388
2389 Py_DECREF(bytesep);
2390 return make_nullbytes_unique(result);
2391}
2392
2393PyDoc_STRVAR(rpartition__doc__,
Ezio Melottidabb5f72010-01-25 11:46:11 +00002394"B.rpartition(sep) -> (head, sep, tail)\n\
Christian Heimes44720832008-05-26 13:01:01 +00002395\n\
2396Searches for the separator sep in B, starting at the end of B,\n\
2397and returns the part before it, the separator itself, and the\n\
2398part after it. If the separator is not found, returns two empty\n\
2399bytearray objects and B.");
2400
2401static PyObject *
2402bytes_rpartition(PyByteArrayObject *self, PyObject *sep_obj)
2403{
2404 PyObject *bytesep, *result;
2405
2406 bytesep = PyByteArray_FromObject(sep_obj);
2407 if (! bytesep)
2408 return NULL;
2409
2410 result = stringlib_rpartition(
2411 (PyObject*) self,
2412 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2413 bytesep,
2414 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2415 );
2416
2417 Py_DECREF(bytesep);
2418 return make_nullbytes_unique(result);
2419}
2420
2421Py_LOCAL_INLINE(PyObject *)
2422rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
2423{
2424 register Py_ssize_t i, j, count=0;
2425 PyObject *str;
2426 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
2427
2428 if (list == NULL)
2429 return NULL;
2430
2431 i = j = len - 1;
2432 while ((i >= 0) && (maxcount-- > 0)) {
2433 for (; i >= 0; i--) {
2434 if (s[i] == ch) {
2435 SPLIT_ADD(s, i + 1, j + 1);
2436 j = i = i - 1;
2437 break;
2438 }
2439 }
2440 }
2441 if (j >= -1) {
2442 SPLIT_ADD(s, 0, j + 1);
2443 }
2444 FIX_PREALLOC_SIZE(list);
2445 if (PyList_Reverse(list) < 0)
2446 goto onError;
2447
2448 return list;
2449
2450 onError:
2451 Py_DECREF(list);
2452 return NULL;
2453}
2454
2455Py_LOCAL_INLINE(PyObject *)
2456rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount)
2457{
2458 register Py_ssize_t i, j, count = 0;
2459 PyObject *str;
2460 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
2461
2462 if (list == NULL)
2463 return NULL;
2464
2465 for (i = j = len - 1; i >= 0; ) {
2466 /* find a token */
2467 while (i >= 0 && ISSPACE(s[i]))
2468 i--;
2469 j = i;
2470 while (i >= 0 && !ISSPACE(s[i]))
2471 i--;
2472 if (j > i) {
2473 if (maxcount-- <= 0)
2474 break;
2475 SPLIT_ADD(s, i + 1, j + 1);
2476 while (i >= 0 && ISSPACE(s[i]))
2477 i--;
2478 j = i;
2479 }
2480 }
2481 if (j >= 0) {
2482 SPLIT_ADD(s, 0, j + 1);
2483 }
2484 FIX_PREALLOC_SIZE(list);
2485 if (PyList_Reverse(list) < 0)
2486 goto onError;
2487
2488 return list;
2489
2490 onError:
2491 Py_DECREF(list);
2492 return NULL;
2493}
2494
2495PyDoc_STRVAR(rsplit__doc__,
2496"B.rsplit(sep[, maxsplit]) -> list of bytearray\n\
2497\n\
2498Return a list of the sections in B, using sep as the delimiter,\n\
2499starting at the end of B and working to the front.\n\
2500If sep is not given, B is split on ASCII whitespace characters\n\
2501(space, tab, return, newline, formfeed, vertical tab).\n\
2502If maxsplit is given, at most maxsplit splits are done.");
2503
2504static PyObject *
2505bytes_rsplit(PyByteArrayObject *self, PyObject *args)
2506{
2507 Py_ssize_t len = PyByteArray_GET_SIZE(self), n, i, j;
2508 Py_ssize_t maxsplit = -1, count = 0;
2509 const char *s = PyByteArray_AS_STRING(self), *sub;
2510 PyObject *list, *str, *subobj = Py_None;
2511 Py_buffer vsub;
2512
2513 if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit))
2514 return NULL;
2515 if (maxsplit < 0)
2516 maxsplit = PY_SSIZE_T_MAX;
2517
2518 if (subobj == Py_None)
2519 return rsplit_whitespace(s, len, maxsplit);
2520
2521 if (_getbuffer(subobj, &vsub) < 0)
2522 return NULL;
2523 sub = vsub.buf;
2524 n = vsub.len;
2525
2526 if (n == 0) {
2527 PyErr_SetString(PyExc_ValueError, "empty separator");
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002528 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002529 return NULL;
2530 }
Amaury Forgeot d'Arc313bda12008-08-17 21:05:18 +00002531 else if (n == 1) {
2532 list = rsplit_char(s, len, sub[0], maxsplit);
2533 PyBuffer_Release(&vsub);
2534 return list;
2535 }
Christian Heimes44720832008-05-26 13:01:01 +00002536
2537 list = PyList_New(PREALLOC_SIZE(maxsplit));
2538 if (list == NULL) {
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002539 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002540 return NULL;
2541 }
2542
2543 j = len;
2544 i = j - n;
2545
2546 while ( (i >= 0) && (maxsplit-- > 0) ) {
2547 for (; i>=0; i--) {
2548 if (Py_STRING_MATCH(s, i, sub, n)) {
2549 SPLIT_ADD(s, i + n, j);
2550 j = i;
2551 i -= n;
2552 break;
2553 }
2554 }
2555 }
2556 SPLIT_ADD(s, 0, j);
2557 FIX_PREALLOC_SIZE(list);
2558 if (PyList_Reverse(list) < 0)
2559 goto onError;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002560 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002561 return list;
2562
2563onError:
2564 Py_DECREF(list);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002565 PyBuffer_Release(&vsub);
Christian Heimes44720832008-05-26 13:01:01 +00002566 return NULL;
2567}
2568
2569PyDoc_STRVAR(reverse__doc__,
2570"B.reverse() -> None\n\
2571\n\
2572Reverse the order of the values in B in place.");
2573static PyObject *
2574bytes_reverse(PyByteArrayObject *self, PyObject *unused)
2575{
2576 char swap, *head, *tail;
2577 Py_ssize_t i, j, n = Py_SIZE(self);
2578
2579 j = n / 2;
2580 head = self->ob_bytes;
2581 tail = head + n - 1;
2582 for (i = 0; i < j; i++) {
2583 swap = *head;
2584 *head++ = *tail;
2585 *tail-- = swap;
2586 }
2587
2588 Py_RETURN_NONE;
2589}
2590
2591PyDoc_STRVAR(insert__doc__,
2592"B.insert(index, int) -> None\n\
2593\n\
2594Insert a single item into the bytearray before the given index.");
2595static PyObject *
2596bytes_insert(PyByteArrayObject *self, PyObject *args)
2597{
Georg Brandl3e483f62008-07-16 22:57:41 +00002598 PyObject *value;
2599 int ival;
Christian Heimes44720832008-05-26 13:01:01 +00002600 Py_ssize_t where, n = Py_SIZE(self);
2601
Georg Brandl3e483f62008-07-16 22:57:41 +00002602 if (!PyArg_ParseTuple(args, "nO:insert", &where, &value))
Christian Heimes44720832008-05-26 13:01:01 +00002603 return NULL;
2604
2605 if (n == PY_SSIZE_T_MAX) {
2606 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson76e96432009-09-06 10:33:12 +00002607 "cannot add more objects to bytearray");
Christian Heimes44720832008-05-26 13:01:01 +00002608 return NULL;
2609 }
Georg Brandl3e483f62008-07-16 22:57:41 +00002610 if (!_getbytevalue(value, &ival))
Christian Heimes44720832008-05-26 13:01:01 +00002611 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00002612 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2613 return NULL;
2614
2615 if (where < 0) {
2616 where += n;
2617 if (where < 0)
2618 where = 0;
2619 }
2620 if (where > n)
2621 where = n;
2622 memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where);
Georg Brandl3e483f62008-07-16 22:57:41 +00002623 self->ob_bytes[where] = ival;
Christian Heimes44720832008-05-26 13:01:01 +00002624
2625 Py_RETURN_NONE;
2626}
2627
2628PyDoc_STRVAR(append__doc__,
2629"B.append(int) -> None\n\
2630\n\
2631Append a single item to the end of B.");
2632static PyObject *
2633bytes_append(PyByteArrayObject *self, PyObject *arg)
2634{
2635 int value;
2636 Py_ssize_t n = Py_SIZE(self);
2637
2638 if (! _getbytevalue(arg, &value))
2639 return NULL;
2640 if (n == PY_SSIZE_T_MAX) {
2641 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson76e96432009-09-06 10:33:12 +00002642 "cannot add more objects to bytearray");
Christian Heimes44720832008-05-26 13:01:01 +00002643 return NULL;
2644 }
2645 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2646 return NULL;
2647
2648 self->ob_bytes[n] = value;
2649
2650 Py_RETURN_NONE;
2651}
2652
2653PyDoc_STRVAR(extend__doc__,
2654"B.extend(iterable int) -> None\n\
2655\n\
2656Append all the elements from the iterator or sequence to the\n\
2657end of B.");
2658static PyObject *
2659bytes_extend(PyByteArrayObject *self, PyObject *arg)
2660{
2661 PyObject *it, *item, *bytes_obj;
2662 Py_ssize_t buf_size = 0, len = 0;
2663 int value;
2664 char *buf;
2665
2666 /* bytes_setslice code only accepts something supporting PEP 3118. */
2667 if (PyObject_CheckBuffer(arg)) {
2668 if (bytes_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1)
2669 return NULL;
2670
2671 Py_RETURN_NONE;
2672 }
2673
2674 it = PyObject_GetIter(arg);
2675 if (it == NULL)
2676 return NULL;
2677
2678 /* Try to determine the length of the argument. 32 is abitrary. */
2679 buf_size = _PyObject_LengthHint(arg, 32);
Georg Brandle9b91212009-04-05 21:26:31 +00002680 if (buf_size == -1) {
2681 Py_DECREF(it);
2682 return NULL;
2683 }
Christian Heimes44720832008-05-26 13:01:01 +00002684
2685 bytes_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
2686 if (bytes_obj == NULL)
2687 return NULL;
2688 buf = PyByteArray_AS_STRING(bytes_obj);
2689
2690 while ((item = PyIter_Next(it)) != NULL) {
2691 if (! _getbytevalue(item, &value)) {
2692 Py_DECREF(item);
2693 Py_DECREF(it);
2694 Py_DECREF(bytes_obj);
2695 return NULL;
2696 }
2697 buf[len++] = value;
2698 Py_DECREF(item);
2699
2700 if (len >= buf_size) {
2701 buf_size = len + (len >> 1) + 1;
2702 if (PyByteArray_Resize((PyObject *)bytes_obj, buf_size) < 0) {
2703 Py_DECREF(it);
2704 Py_DECREF(bytes_obj);
2705 return NULL;
2706 }
2707 /* Recompute the `buf' pointer, since the resizing operation may
2708 have invalidated it. */
2709 buf = PyByteArray_AS_STRING(bytes_obj);
2710 }
2711 }
2712 Py_DECREF(it);
2713
2714 /* Resize down to exact size. */
2715 if (PyByteArray_Resize((PyObject *)bytes_obj, len) < 0) {
2716 Py_DECREF(bytes_obj);
2717 return NULL;
2718 }
2719
2720 if (bytes_setslice(self, Py_SIZE(self), Py_SIZE(self), bytes_obj) == -1)
2721 return NULL;
2722 Py_DECREF(bytes_obj);
2723
2724 Py_RETURN_NONE;
2725}
2726
2727PyDoc_STRVAR(pop__doc__,
2728"B.pop([index]) -> int\n\
2729\n\
2730Remove and return a single item from B. If no index\n\
Andrew M. Kuchlingd8972642008-06-21 13:29:12 +00002731argument is given, will pop the last value.");
Christian Heimes44720832008-05-26 13:01:01 +00002732static PyObject *
2733bytes_pop(PyByteArrayObject *self, PyObject *args)
2734{
2735 int value;
2736 Py_ssize_t where = -1, n = Py_SIZE(self);
2737
2738 if (!PyArg_ParseTuple(args, "|n:pop", &where))
2739 return NULL;
2740
2741 if (n == 0) {
2742 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson76e96432009-09-06 10:33:12 +00002743 "cannot pop an empty bytearray");
Christian Heimes44720832008-05-26 13:01:01 +00002744 return NULL;
2745 }
2746 if (where < 0)
2747 where += Py_SIZE(self);
2748 if (where < 0 || where >= Py_SIZE(self)) {
2749 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2750 return NULL;
2751 }
Antoine Pitrou599db7f2008-12-07 00:07:51 +00002752 if (!_canresize(self))
2753 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00002754
2755 value = self->ob_bytes[where];
2756 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where);
2757 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2758 return NULL;
2759
Mark Dickinsonb61c0352009-09-06 10:05:28 +00002760 return PyInt_FromLong((unsigned char)value);
Christian Heimes44720832008-05-26 13:01:01 +00002761}
2762
2763PyDoc_STRVAR(remove__doc__,
2764"B.remove(int) -> None\n\
2765\n\
2766Remove the first occurance of a value in B.");
2767static PyObject *
2768bytes_remove(PyByteArrayObject *self, PyObject *arg)
2769{
2770 int value;
2771 Py_ssize_t where, n = Py_SIZE(self);
2772
2773 if (! _getbytevalue(arg, &value))
2774 return NULL;
2775
2776 for (where = 0; where < n; where++) {
2777 if (self->ob_bytes[where] == value)
2778 break;
2779 }
2780 if (where == n) {
Mark Dickinson76e96432009-09-06 10:33:12 +00002781 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes44720832008-05-26 13:01:01 +00002782 return NULL;
2783 }
Antoine Pitrou599db7f2008-12-07 00:07:51 +00002784 if (!_canresize(self))
2785 return NULL;
Christian Heimes44720832008-05-26 13:01:01 +00002786
2787 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where);
2788 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2789 return NULL;
2790
2791 Py_RETURN_NONE;
2792}
2793
2794/* XXX These two helpers could be optimized if argsize == 1 */
2795
2796static Py_ssize_t
2797lstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
2798 void *argptr, Py_ssize_t argsize)
2799{
2800 Py_ssize_t i = 0;
2801 while (i < mysize && memchr(argptr, myptr[i], argsize))
2802 i++;
2803 return i;
2804}
2805
2806static Py_ssize_t
2807rstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
2808 void *argptr, Py_ssize_t argsize)
2809{
2810 Py_ssize_t i = mysize - 1;
2811 while (i >= 0 && memchr(argptr, myptr[i], argsize))
2812 i--;
2813 return i + 1;
2814}
2815
2816PyDoc_STRVAR(strip__doc__,
2817"B.strip([bytes]) -> bytearray\n\
2818\n\
2819Strip leading and trailing bytes contained in the argument.\n\
2820If the argument is omitted, strip ASCII whitespace.");
2821static PyObject *
2822bytes_strip(PyByteArrayObject *self, PyObject *args)
2823{
2824 Py_ssize_t left, right, mysize, argsize;
2825 void *myptr, *argptr;
2826 PyObject *arg = Py_None;
2827 Py_buffer varg;
2828 if (!PyArg_ParseTuple(args, "|O:strip", &arg))
2829 return NULL;
2830 if (arg == Py_None) {
2831 argptr = "\t\n\r\f\v ";
2832 argsize = 6;
2833 }
2834 else {
2835 if (_getbuffer(arg, &varg) < 0)
2836 return NULL;
2837 argptr = varg.buf;
2838 argsize = varg.len;
2839 }
2840 myptr = self->ob_bytes;
2841 mysize = Py_SIZE(self);
2842 left = lstrip_helper(myptr, mysize, argptr, argsize);
2843 if (left == mysize)
2844 right = left;
2845 else
2846 right = rstrip_helper(myptr, mysize, argptr, argsize);
2847 if (arg != Py_None)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002848 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00002849 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2850}
2851
2852PyDoc_STRVAR(lstrip__doc__,
2853"B.lstrip([bytes]) -> bytearray\n\
2854\n\
2855Strip leading bytes contained in the argument.\n\
2856If the argument is omitted, strip leading ASCII whitespace.");
2857static PyObject *
2858bytes_lstrip(PyByteArrayObject *self, PyObject *args)
2859{
2860 Py_ssize_t left, right, mysize, argsize;
2861 void *myptr, *argptr;
2862 PyObject *arg = Py_None;
2863 Py_buffer varg;
2864 if (!PyArg_ParseTuple(args, "|O:lstrip", &arg))
2865 return NULL;
2866 if (arg == Py_None) {
2867 argptr = "\t\n\r\f\v ";
2868 argsize = 6;
2869 }
2870 else {
2871 if (_getbuffer(arg, &varg) < 0)
2872 return NULL;
2873 argptr = varg.buf;
2874 argsize = varg.len;
2875 }
2876 myptr = self->ob_bytes;
2877 mysize = Py_SIZE(self);
2878 left = lstrip_helper(myptr, mysize, argptr, argsize);
2879 right = mysize;
2880 if (arg != Py_None)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002881 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00002882 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2883}
2884
2885PyDoc_STRVAR(rstrip__doc__,
2886"B.rstrip([bytes]) -> bytearray\n\
2887\n\
2888Strip trailing bytes contained in the argument.\n\
2889If the argument is omitted, strip trailing ASCII whitespace.");
2890static PyObject *
2891bytes_rstrip(PyByteArrayObject *self, PyObject *args)
2892{
2893 Py_ssize_t left, right, mysize, argsize;
2894 void *myptr, *argptr;
2895 PyObject *arg = Py_None;
2896 Py_buffer varg;
2897 if (!PyArg_ParseTuple(args, "|O:rstrip", &arg))
2898 return NULL;
2899 if (arg == Py_None) {
2900 argptr = "\t\n\r\f\v ";
2901 argsize = 6;
2902 }
2903 else {
2904 if (_getbuffer(arg, &varg) < 0)
2905 return NULL;
2906 argptr = varg.buf;
2907 argsize = varg.len;
2908 }
2909 myptr = self->ob_bytes;
2910 mysize = Py_SIZE(self);
2911 left = 0;
2912 right = rstrip_helper(myptr, mysize, argptr, argsize);
2913 if (arg != Py_None)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00002914 PyBuffer_Release(&varg);
Christian Heimes44720832008-05-26 13:01:01 +00002915 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2916}
2917
2918PyDoc_STRVAR(decode_doc,
2919"B.decode([encoding[, errors]]) -> unicode object.\n\
2920\n\
2921Decodes B using the codec registered for encoding. encoding defaults\n\
2922to the default encoding. errors may be given to set a different error\n\
2923handling scheme. Default is 'strict' meaning that encoding errors raise\n\
2924a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
2925as well as any other name registered with codecs.register_error that is\n\
2926able to handle UnicodeDecodeErrors.");
2927
2928static PyObject *
2929bytes_decode(PyObject *self, PyObject *args)
2930{
2931 const char *encoding = NULL;
2932 const char *errors = NULL;
2933
2934 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
2935 return NULL;
2936 if (encoding == NULL)
2937 encoding = PyUnicode_GetDefaultEncoding();
2938 return PyCodec_Decode(self, encoding, errors);
2939}
2940
2941PyDoc_STRVAR(alloc_doc,
2942"B.__alloc__() -> int\n\
2943\n\
2944Returns the number of bytes actually allocated.");
2945
2946static PyObject *
2947bytes_alloc(PyByteArrayObject *self)
2948{
2949 return PyInt_FromSsize_t(self->ob_alloc);
2950}
2951
2952PyDoc_STRVAR(join_doc,
2953"B.join(iterable_of_bytes) -> bytes\n\
2954\n\
2955Concatenates any number of bytearray objects, with B in between each pair.");
2956
2957static PyObject *
2958bytes_join(PyByteArrayObject *self, PyObject *it)
2959{
2960 PyObject *seq;
2961 Py_ssize_t mysize = Py_SIZE(self);
2962 Py_ssize_t i;
2963 Py_ssize_t n;
2964 PyObject **items;
2965 Py_ssize_t totalsize = 0;
2966 PyObject *result;
2967 char *dest;
2968
2969 seq = PySequence_Fast(it, "can only join an iterable");
2970 if (seq == NULL)
2971 return NULL;
2972 n = PySequence_Fast_GET_SIZE(seq);
2973 items = PySequence_Fast_ITEMS(seq);
2974
2975 /* Compute the total size, and check that they are all bytes */
2976 /* XXX Shouldn't we use _getbuffer() on these items instead? */
2977 for (i = 0; i < n; i++) {
2978 PyObject *obj = items[i];
2979 if (!PyByteArray_Check(obj) && !PyBytes_Check(obj)) {
2980 PyErr_Format(PyExc_TypeError,
2981 "can only join an iterable of bytes "
2982 "(item %ld has type '%.100s')",
2983 /* XXX %ld isn't right on Win64 */
2984 (long)i, Py_TYPE(obj)->tp_name);
2985 goto error;
2986 }
2987 if (i > 0)
2988 totalsize += mysize;
2989 totalsize += Py_SIZE(obj);
2990 if (totalsize < 0) {
2991 PyErr_NoMemory();
2992 goto error;
2993 }
2994 }
2995
2996 /* Allocate the result, and copy the bytes */
2997 result = PyByteArray_FromStringAndSize(NULL, totalsize);
2998 if (result == NULL)
2999 goto error;
3000 dest = PyByteArray_AS_STRING(result);
3001 for (i = 0; i < n; i++) {
3002 PyObject *obj = items[i];
3003 Py_ssize_t size = Py_SIZE(obj);
3004 char *buf;
3005 if (PyByteArray_Check(obj))
3006 buf = PyByteArray_AS_STRING(obj);
3007 else
3008 buf = PyBytes_AS_STRING(obj);
3009 if (i) {
3010 memcpy(dest, self->ob_bytes, mysize);
3011 dest += mysize;
3012 }
3013 memcpy(dest, buf, size);
3014 dest += size;
3015 }
3016
3017 /* Done */
3018 Py_DECREF(seq);
3019 return result;
3020
3021 /* Error handling */
3022 error:
3023 Py_DECREF(seq);
3024 return NULL;
3025}
3026
3027PyDoc_STRVAR(fromhex_doc,
3028"bytearray.fromhex(string) -> bytearray\n\
3029\n\
3030Create a bytearray object from a string of hexadecimal numbers.\n\
3031Spaces between two numbers are accepted.\n\
3032Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
3033
3034static int
3035hex_digit_to_int(Py_UNICODE c)
3036{
3037 if (c >= 128)
3038 return -1;
3039 if (ISDIGIT(c))
3040 return c - '0';
3041 else {
3042 if (ISUPPER(c))
3043 c = TOLOWER(c);
3044 if (c >= 'a' && c <= 'f')
3045 return c - 'a' + 10;
3046 }
3047 return -1;
3048}
3049
3050static PyObject *
3051bytes_fromhex(PyObject *cls, PyObject *args)
3052{
3053 PyObject *newbytes, *hexobj;
3054 char *buf;
3055 Py_UNICODE *hex;
3056 Py_ssize_t hexlen, byteslen, i, j;
3057 int top, bot;
3058
3059 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
3060 return NULL;
3061 assert(PyUnicode_Check(hexobj));
3062 hexlen = PyUnicode_GET_SIZE(hexobj);
3063 hex = PyUnicode_AS_UNICODE(hexobj);
3064 byteslen = hexlen/2; /* This overestimates if there are spaces */
3065 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
3066 if (!newbytes)
3067 return NULL;
3068 buf = PyByteArray_AS_STRING(newbytes);
3069 for (i = j = 0; i < hexlen; i += 2) {
3070 /* skip over spaces in the input */
3071 while (hex[i] == ' ')
3072 i++;
3073 if (i >= hexlen)
3074 break;
3075 top = hex_digit_to_int(hex[i]);
3076 bot = hex_digit_to_int(hex[i+1]);
3077 if (top == -1 || bot == -1) {
3078 PyErr_Format(PyExc_ValueError,
3079 "non-hexadecimal number found in "
3080 "fromhex() arg at position %zd", i);
3081 goto error;
3082 }
3083 buf[j++] = (top << 4) + bot;
3084 }
3085 if (PyByteArray_Resize(newbytes, j) < 0)
3086 goto error;
3087 return newbytes;
3088
3089 error:
3090 Py_DECREF(newbytes);
3091 return NULL;
3092}
3093
3094PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3095
3096static PyObject *
3097bytes_reduce(PyByteArrayObject *self)
3098{
3099 PyObject *latin1, *dict;
3100 if (self->ob_bytes)
3101 latin1 = PyUnicode_DecodeLatin1(self->ob_bytes,
3102 Py_SIZE(self), NULL);
3103 else
3104 latin1 = PyUnicode_FromString("");
3105
3106 dict = PyObject_GetAttrString((PyObject *)self, "__dict__");
3107 if (dict == NULL) {
3108 PyErr_Clear();
3109 dict = Py_None;
3110 Py_INCREF(dict);
3111 }
3112
3113 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
3114}
3115
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003116PyDoc_STRVAR(sizeof_doc,
3117"B.__sizeof__() -> int\n\
3118 \n\
3119Returns the size of B in memory, in bytes");
3120static PyObject *
3121bytes_sizeof(PyByteArrayObject *self)
3122{
Georg Brandle9b91212009-04-05 21:26:31 +00003123 Py_ssize_t res;
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003124
Georg Brandle9b91212009-04-05 21:26:31 +00003125 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
3126 return PyInt_FromSsize_t(res);
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003127}
3128
Christian Heimes44720832008-05-26 13:01:01 +00003129static PySequenceMethods bytes_as_sequence = {
3130 (lenfunc)bytes_length, /* sq_length */
3131 (binaryfunc)PyByteArray_Concat, /* sq_concat */
3132 (ssizeargfunc)bytes_repeat, /* sq_repeat */
3133 (ssizeargfunc)bytes_getitem, /* sq_item */
3134 0, /* sq_slice */
3135 (ssizeobjargproc)bytes_setitem, /* sq_ass_item */
3136 0, /* sq_ass_slice */
3137 (objobjproc)bytes_contains, /* sq_contains */
3138 (binaryfunc)bytes_iconcat, /* sq_inplace_concat */
3139 (ssizeargfunc)bytes_irepeat, /* sq_inplace_repeat */
3140};
3141
3142static PyMappingMethods bytes_as_mapping = {
3143 (lenfunc)bytes_length,
3144 (binaryfunc)bytes_subscript,
3145 (objobjargproc)bytes_ass_subscript,
3146};
3147
3148static PyBufferProcs bytes_as_buffer = {
3149 (readbufferproc)bytes_buffer_getreadbuf,
3150 (writebufferproc)bytes_buffer_getwritebuf,
3151 (segcountproc)bytes_buffer_getsegcount,
3152 (charbufferproc)bytes_buffer_getcharbuf,
3153 (getbufferproc)bytes_getbuffer,
3154 (releasebufferproc)bytes_releasebuffer,
3155};
3156
3157static PyMethodDef
3158bytes_methods[] = {
3159 {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc},
3160 {"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc},
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00003161 {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, sizeof_doc},
Christian Heimes44720832008-05-26 13:01:01 +00003162 {"append", (PyCFunction)bytes_append, METH_O, append__doc__},
3163 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
3164 _Py_capitalize__doc__},
3165 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
3166 {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__},
3167 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc},
3168 {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, endswith__doc__},
3169 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS,
3170 expandtabs__doc__},
3171 {"extend", (PyCFunction)bytes_extend, METH_O, extend__doc__},
3172 {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__},
3173 {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS,
3174 fromhex_doc},
3175 {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__},
3176 {"insert", (PyCFunction)bytes_insert, METH_VARARGS, insert__doc__},
3177 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
3178 _Py_isalnum__doc__},
3179 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
3180 _Py_isalpha__doc__},
3181 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
3182 _Py_isdigit__doc__},
3183 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
3184 _Py_islower__doc__},
3185 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
3186 _Py_isspace__doc__},
3187 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
3188 _Py_istitle__doc__},
3189 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
3190 _Py_isupper__doc__},
3191 {"join", (PyCFunction)bytes_join, METH_O, join_doc},
3192 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
3193 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
3194 {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__},
3195 {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__},
3196 {"pop", (PyCFunction)bytes_pop, METH_VARARGS, pop__doc__},
3197 {"remove", (PyCFunction)bytes_remove, METH_O, remove__doc__},
3198 {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__},
3199 {"reverse", (PyCFunction)bytes_reverse, METH_NOARGS, reverse__doc__},
3200 {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__},
3201 {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__},
3202 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
3203 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, rpartition__doc__},
3204 {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__},
3205 {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__},
3206 {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__},
3207 {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS,
3208 splitlines__doc__},
3209 {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS ,
3210 startswith__doc__},
3211 {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__},
3212 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
3213 _Py_swapcase__doc__},
3214 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
3215 {"translate", (PyCFunction)bytes_translate, METH_VARARGS,
3216 translate__doc__},
3217 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
3218 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
3219 {NULL}
3220};
3221
3222PyDoc_STRVAR(bytes_doc,
3223"bytearray(iterable_of_ints) -> bytearray.\n\
3224bytearray(string, encoding[, errors]) -> bytearray.\n\
3225bytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray.\n\
3226bytearray(memory_view) -> bytearray.\n\
3227\n\
3228Construct an mutable bytearray object from:\n\
3229 - an iterable yielding integers in range(256)\n\
3230 - a text string encoded using the specified encoding\n\
3231 - a bytes or a bytearray object\n\
3232 - any object implementing the buffer API.\n\
3233\n\
3234bytearray(int) -> bytearray.\n\
3235\n\
3236Construct a zero-initialized bytearray of the given length.");
3237
3238
3239static PyObject *bytes_iter(PyObject *seq);
3240
3241PyTypeObject PyByteArray_Type = {
3242 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3243 "bytearray",
3244 sizeof(PyByteArrayObject),
3245 0,
3246 (destructor)bytes_dealloc, /* tp_dealloc */
3247 0, /* tp_print */
3248 0, /* tp_getattr */
3249 0, /* tp_setattr */
3250 0, /* tp_compare */
3251 (reprfunc)bytes_repr, /* tp_repr */
3252 0, /* tp_as_number */
3253 &bytes_as_sequence, /* tp_as_sequence */
3254 &bytes_as_mapping, /* tp_as_mapping */
3255 0, /* tp_hash */
3256 0, /* tp_call */
3257 bytes_str, /* tp_str */
3258 PyObject_GenericGetAttr, /* tp_getattro */
3259 0, /* tp_setattro */
3260 &bytes_as_buffer, /* tp_as_buffer */
3261 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
3262 Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */
3263 bytes_doc, /* tp_doc */
3264 0, /* tp_traverse */
3265 0, /* tp_clear */
3266 (richcmpfunc)bytes_richcompare, /* tp_richcompare */
3267 0, /* tp_weaklistoffset */
3268 bytes_iter, /* tp_iter */
3269 0, /* tp_iternext */
3270 bytes_methods, /* tp_methods */
3271 0, /* tp_members */
3272 0, /* tp_getset */
3273 0, /* tp_base */
3274 0, /* tp_dict */
3275 0, /* tp_descr_get */
3276 0, /* tp_descr_set */
3277 0, /* tp_dictoffset */
3278 (initproc)bytes_init, /* tp_init */
3279 PyType_GenericAlloc, /* tp_alloc */
3280 PyType_GenericNew, /* tp_new */
3281 PyObject_Del, /* tp_free */
3282};
3283
3284/*********************** Bytes Iterator ****************************/
3285
3286typedef struct {
3287 PyObject_HEAD
3288 Py_ssize_t it_index;
3289 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
3290} bytesiterobject;
3291
3292static void
3293bytesiter_dealloc(bytesiterobject *it)
3294{
3295 _PyObject_GC_UNTRACK(it);
3296 Py_XDECREF(it->it_seq);
3297 PyObject_GC_Del(it);
3298}
3299
3300static int
3301bytesiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
3302{
3303 Py_VISIT(it->it_seq);
3304 return 0;
3305}
3306
3307static PyObject *
3308bytesiter_next(bytesiterobject *it)
3309{
3310 PyByteArrayObject *seq;
3311 PyObject *item;
3312
3313 assert(it != NULL);
3314 seq = it->it_seq;
3315 if (seq == NULL)
3316 return NULL;
3317 assert(PyByteArray_Check(seq));
3318
3319 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
3320 item = PyInt_FromLong(
3321 (unsigned char)seq->ob_bytes[it->it_index]);
3322 if (item != NULL)
3323 ++it->it_index;
3324 return item;
3325 }
3326
3327 Py_DECREF(seq);
3328 it->it_seq = NULL;
3329 return NULL;
3330}
3331
3332static PyObject *
3333bytesiter_length_hint(bytesiterobject *it)
3334{
3335 Py_ssize_t len = 0;
3336 if (it->it_seq)
3337 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
3338 return PyInt_FromSsize_t(len);
3339}
3340
3341PyDoc_STRVAR(length_hint_doc,
3342 "Private method returning an estimate of len(list(it)).");
3343
3344static PyMethodDef bytesiter_methods[] = {
3345 {"__length_hint__", (PyCFunction)bytesiter_length_hint, METH_NOARGS,
3346 length_hint_doc},
3347 {NULL, NULL} /* sentinel */
3348};
3349
3350PyTypeObject PyByteArrayIter_Type = {
3351 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3352 "bytearray_iterator", /* tp_name */
3353 sizeof(bytesiterobject), /* tp_basicsize */
3354 0, /* tp_itemsize */
3355 /* methods */
3356 (destructor)bytesiter_dealloc, /* tp_dealloc */
3357 0, /* tp_print */
3358 0, /* tp_getattr */
3359 0, /* tp_setattr */
3360 0, /* tp_compare */
3361 0, /* tp_repr */
3362 0, /* tp_as_number */
3363 0, /* tp_as_sequence */
3364 0, /* tp_as_mapping */
3365 0, /* tp_hash */
3366 0, /* tp_call */
3367 0, /* tp_str */
3368 PyObject_GenericGetAttr, /* tp_getattro */
3369 0, /* tp_setattro */
3370 0, /* tp_as_buffer */
3371 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3372 0, /* tp_doc */
3373 (traverseproc)bytesiter_traverse, /* tp_traverse */
3374 0, /* tp_clear */
3375 0, /* tp_richcompare */
3376 0, /* tp_weaklistoffset */
3377 PyObject_SelfIter, /* tp_iter */
3378 (iternextfunc)bytesiter_next, /* tp_iternext */
3379 bytesiter_methods, /* tp_methods */
3380 0,
3381};
3382
3383static PyObject *
3384bytes_iter(PyObject *seq)
3385{
3386 bytesiterobject *it;
3387
3388 if (!PyByteArray_Check(seq)) {
3389 PyErr_BadInternalCall();
3390 return NULL;
3391 }
3392 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3393 if (it == NULL)
3394 return NULL;
3395 it->it_index = 0;
3396 Py_INCREF(seq);
3397 it->it_seq = (PyByteArrayObject *)seq;
3398 _PyObject_GC_TRACK(it);
3399 return (PyObject *)it;
3400}