1
/* PyByteArray (bytearray) implementation */
3
#define PY_SSIZE_T_CLEAN
5
#include "structmember.h"
6
#include "bytes_methods.h"
8
char _PyByteArray_empty_string[] = "";
11
PyByteArray_Fini(void)
16
PyByteArray_Init(void)
21
/* end nullbytes support */
26
_getbytevalue(PyObject* arg, int *value)
30
if (PyLong_Check(arg)) {
31
face_value = PyLong_AsLong(arg);
33
PyObject *index = PyNumber_Index(arg);
35
PyErr_Format(PyExc_TypeError, "an integer is required");
39
face_value = PyLong_AsLong(index);
43
if (face_value < 0 || face_value >= 256) {
44
/* this includes the OverflowError in case the long is too large */
45
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
55
bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
63
ptr = (void *) PyByteArray_AS_STRING(obj);
64
ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
72
bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
78
_getbuffer(PyObject *obj, Py_buffer *view)
80
PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer;
82
if (buffer == NULL || buffer->bf_getbuffer == NULL)
84
PyErr_Format(PyExc_TypeError,
85
"Type %.100s doesn't support the buffer API",
86
Py_TYPE(obj)->tp_name);
90
if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
96
_canresize(PyByteArrayObject *self)
98
if (self->ob_exports > 0) {
99
PyErr_SetString(PyExc_BufferError,
100
"Existing exports of data: object cannot be re-sized");
106
/* Direct API functions */
109
PyByteArray_FromObject(PyObject *input)
111
return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
116
PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
118
PyByteArrayObject *new;
122
PyErr_SetString(PyExc_SystemError,
123
"Negative size passed to PyByteArray_FromStringAndSize");
127
/* Prevent buffer overflow when setting alloc to size+1. */
128
if (size == PY_SSIZE_T_MAX) {
129
return PyErr_NoMemory();
132
new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
137
new->ob_bytes = NULL;
142
new->ob_bytes = PyObject_Malloc(alloc);
143
if (new->ob_bytes == NULL) {
145
return PyErr_NoMemory();
147
if (bytes != NULL && size > 0)
148
memcpy(new->ob_bytes, bytes, size);
149
new->ob_bytes[size] = '\0'; /* Trailing null byte */
152
new->ob_alloc = alloc;
153
new->ob_start = new->ob_bytes;
156
return (PyObject *)new;
160
PyByteArray_Size(PyObject *self)
162
assert(self != NULL);
163
assert(PyByteArray_Check(self));
165
return PyByteArray_GET_SIZE(self);
169
PyByteArray_AsString(PyObject *self)
171
assert(self != NULL);
172
assert(PyByteArray_Check(self));
174
return PyByteArray_AS_STRING(self);
178
PyByteArray_Resize(PyObject *self, Py_ssize_t size)
181
PyByteArrayObject *obj = ((PyByteArrayObject *)self);
182
Py_ssize_t alloc = obj->ob_alloc;
183
Py_ssize_t logical_offset = obj->ob_start - obj->ob_bytes;
185
assert(self != NULL);
186
assert(PyByteArray_Check(self));
188
assert(logical_offset >= 0);
189
assert(logical_offset <= alloc);
191
if (size == Py_SIZE(self)) {
194
if (!_canresize(obj)) {
198
if (size + logical_offset + 1 < alloc) {
199
/* Current buffer is large enough to host the requested size,
200
decide on a strategy. */
201
if (size < alloc / 2) {
202
/* Major downsize; resize down to exact size */
206
/* Minor downsize; quick exit */
207
Py_SIZE(self) = size;
208
PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
213
/* Need growing, decide on a strategy */
214
if (size <= alloc * 1.125) {
215
/* Moderate upsize; overallocate similar to list_resize() */
216
alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
219
/* Major upsize; resize up to exact size */
224
if (logical_offset > 0) {
225
sval = PyObject_Malloc(alloc);
230
memcpy(sval, PyByteArray_AS_STRING(self), Py_MIN(size, Py_SIZE(self)));
231
PyObject_Free(obj->ob_bytes);
234
sval = PyObject_Realloc(obj->ob_bytes, alloc);
241
obj->ob_bytes = obj->ob_start = sval;
242
Py_SIZE(self) = size;
243
obj->ob_alloc = alloc;
244
obj->ob_bytes[size] = '\0'; /* Trailing null byte */
250
PyByteArray_Concat(PyObject *a, PyObject *b)
254
PyByteArrayObject *result = NULL;
258
if (_getbuffer(a, &va) < 0 ||
259
_getbuffer(b, &vb) < 0) {
260
PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
261
Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
265
size = va.len + vb.len;
271
result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
272
if (result != NULL) {
273
memcpy(result->ob_bytes, va.buf, va.len);
274
memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
279
PyBuffer_Release(&va);
281
PyBuffer_Release(&vb);
282
return (PyObject *)result;
285
/* Functions stuffed into the type object */
288
bytearray_length(PyByteArrayObject *self)
290
return Py_SIZE(self);
294
bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
300
if (_getbuffer(other, &vo) < 0) {
301
PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
302
Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
306
mysize = Py_SIZE(self);
307
size = mysize + vo.len;
309
PyBuffer_Release(&vo);
310
return PyErr_NoMemory();
312
if (size < self->ob_alloc) {
313
Py_SIZE(self) = size;
314
PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; /* Trailing null byte */
316
else if (PyByteArray_Resize((PyObject *)self, size) < 0) {
317
PyBuffer_Release(&vo);
320
memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len);
321
PyBuffer_Release(&vo);
323
return (PyObject *)self;
327
bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
329
PyByteArrayObject *result;
335
mysize = Py_SIZE(self);
336
if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
337
return PyErr_NoMemory();
338
size = mysize * count;
339
result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
340
if (result != NULL && size != 0) {
342
memset(result->ob_bytes, self->ob_bytes[0], size);
345
for (i = 0; i < count; i++)
346
memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
349
return (PyObject *)result;
353
bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
361
mysize = Py_SIZE(self);
362
if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
363
return PyErr_NoMemory();
364
size = mysize * count;
365
if (PyByteArray_Resize((PyObject *)self, size) < 0)
368
buf = PyByteArray_AS_STRING(self);
370
memset(buf, buf[0], size);
373
for (i = 1; i < count; i++)
374
memcpy(buf + i*mysize, buf, mysize);
378
return (PyObject *)self;
382
bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
386
if (i < 0 || i >= Py_SIZE(self)) {
387
PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
390
return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
394
bytearray_subscript(PyByteArrayObject *self, PyObject *index)
396
if (PyIndex_Check(index)) {
397
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
399
if (i == -1 && PyErr_Occurred())
403
i += PyByteArray_GET_SIZE(self);
405
if (i < 0 || i >= Py_SIZE(self)) {
406
PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
409
return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
411
else if (PySlice_Check(index)) {
412
Py_ssize_t start, stop, step, slicelength, cur, i;
413
if (PySlice_GetIndicesEx(index,
414
PyByteArray_GET_SIZE(self),
415
&start, &stop, &step, &slicelength) < 0) {
419
if (slicelength <= 0)
420
return PyByteArray_FromStringAndSize("", 0);
421
else if (step == 1) {
422
return PyByteArray_FromStringAndSize(
423
PyByteArray_AS_STRING(self) + start, slicelength);
426
char *source_buf = PyByteArray_AS_STRING(self);
430
result = PyByteArray_FromStringAndSize(NULL, slicelength);
434
result_buf = PyByteArray_AS_STRING(result);
435
for (cur = start, i = 0; i < slicelength;
437
result_buf[i] = source_buf[cur];
443
PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers");
449
bytearray_setslice_linear(PyByteArrayObject *self,
450
Py_ssize_t lo, Py_ssize_t hi,
451
char *bytes, Py_ssize_t bytes_len)
453
Py_ssize_t avail = hi - lo;
454
char *buf = PyByteArray_AS_STRING(self);
455
Py_ssize_t growth = bytes_len - avail;
460
if (!_canresize(self))
464
/* Shrink the buffer by advancing its logical start */
465
self->ob_start -= growth;
468
| |<----avail----->|<-----tail------>|
469
| |<-bytes_len->|<-----tail------>|
470
0 new_lo new_hi new_size
476
| |<----avail----->|<-----tomove------>|
477
| |<-bytes_len->|<-----tomove------>|
480
memmove(buf + lo + bytes_len, buf + hi,
483
if (PyByteArray_Resize((PyObject *)self,
484
Py_SIZE(self) + growth) < 0) {
485
/* Issue #19578: Handling the memory allocation failure here is
486
tricky here because the bytearray object has already been
487
modified. Depending on growth and lo, the behaviour is
490
If growth < 0 and lo != 0, the operation is completed, but a
491
MemoryError is still raised and the memory block is not
492
shrinked. Otherwise, the bytearray is restored in its previous
493
state and a MemoryError is raised. */
495
self->ob_start += growth;
498
/* memmove() removed bytes, the bytearray object cannot be
499
restored in its previous state. */
500
Py_SIZE(self) += growth;
503
buf = PyByteArray_AS_STRING(self);
505
else if (growth > 0) {
506
if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
511
if (PyByteArray_Resize((PyObject *)self,
512
Py_SIZE(self) + growth) < 0) {
515
buf = PyByteArray_AS_STRING(self);
516
/* Make the place for the additional bytes */
519
| |<-avail->|<-----tomove------>|
520
| |<---bytes_len-->|<-----tomove------>|
523
memmove(buf + lo + bytes_len, buf + hi,
524
Py_SIZE(self) - lo - bytes_len);
528
memcpy(buf + lo, bytes, bytes_len);
533
bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
542
if (values == (PyObject *)self) {
543
/* Make a copy and call this function recursively */
545
values = PyByteArray_FromObject(values);
548
err = bytearray_setslice(self, lo, hi, values);
552
if (values == NULL) {
558
if (_getbuffer(values, &vbytes) < 0) {
559
PyErr_Format(PyExc_TypeError,
560
"can't set bytearray slice from %.100s",
561
Py_TYPE(values)->tp_name);
572
if (hi > Py_SIZE(self))
575
res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
576
if (vbytes.len != -1)
577
PyBuffer_Release(&vbytes);
582
bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
589
if (i < 0 || i >= Py_SIZE(self)) {
590
PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
595
return bytearray_setslice(self, i, i+1, NULL);
597
if (!_getbytevalue(value, &ival))
600
PyByteArray_AS_STRING(self)[i] = ival;
605
bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
607
Py_ssize_t start, stop, step, slicelen, needed;
609
buf = PyByteArray_AS_STRING(self);
611
if (PyIndex_Check(index)) {
612
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
614
if (i == -1 && PyErr_Occurred())
618
i += PyByteArray_GET_SIZE(self);
620
if (i < 0 || i >= Py_SIZE(self)) {
621
PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
625
if (values == NULL) {
626
/* Fall through to slice assignment */
634
if (!_getbytevalue(values, &ival))
640
else if (PySlice_Check(index)) {
641
if (PySlice_GetIndicesEx(index,
642
PyByteArray_GET_SIZE(self),
643
&start, &stop, &step, &slicelen) < 0) {
648
PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer");
652
if (values == NULL) {
656
else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
658
if (PyNumber_Check(values) || PyUnicode_Check(values)) {
659
PyErr_SetString(PyExc_TypeError,
660
"can assign only bytes, buffers, or iterables "
661
"of ints in range(0, 256)");
664
/* Make a copy and call this function recursively */
665
values = PyByteArray_FromObject(values);
668
err = bytearray_ass_subscript(self, index, values);
673
assert(PyByteArray_Check(values));
674
bytes = PyByteArray_AS_STRING(values);
675
needed = Py_SIZE(values);
677
/* Make sure b[5:2] = ... inserts before 5, not before 2. */
678
if ((step < 0 && start < stop) ||
679
(step > 0 && start > stop))
682
return bytearray_setslice_linear(self, start, stop, bytes, needed);
690
if (!_canresize(self))
694
/* Nothing to do here. */
699
start = stop + step * (slicelen - 1) - 1;
702
for (cur = start, i = 0;
703
i < slicelen; cur += step, i++) {
704
Py_ssize_t lim = step - 1;
706
if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
707
lim = PyByteArray_GET_SIZE(self) - cur - 1;
709
memmove(buf + cur - i,
712
/* Move the tail of the bytes, in one chunk */
713
cur = start + (size_t)slicelen*step;
714
if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
715
memmove(buf + cur - slicelen,
717
PyByteArray_GET_SIZE(self) - cur);
719
if (PyByteArray_Resize((PyObject *)self,
720
PyByteArray_GET_SIZE(self) - slicelen) < 0)
730
if (needed != slicelen) {
731
PyErr_Format(PyExc_ValueError,
732
"attempt to assign bytes of size %zd "
733
"to extended slice of size %zd",
737
for (cur = start, i = 0; i < slicelen; cur += step, i++)
745
bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
747
static char *kwlist[] = {"source", "encoding", "errors", 0};
748
PyObject *arg = NULL;
749
const char *encoding = NULL;
750
const char *errors = NULL;
753
PyObject *(*iternext)(PyObject *);
755
if (Py_SIZE(self) != 0) {
756
/* Empty previous contents (yes, do this first of all!) */
757
if (PyByteArray_Resize((PyObject *)self, 0) < 0)
761
/* Parse arguments */
762
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
763
&arg, &encoding, &errors))
766
/* Make a quick exit if no first argument */
768
if (encoding != NULL || errors != NULL) {
769
PyErr_SetString(PyExc_TypeError,
770
"encoding or errors without sequence argument");
776
if (PyUnicode_Check(arg)) {
777
/* Encode via the codec registry */
778
PyObject *encoded, *new;
779
if (encoding == NULL) {
780
PyErr_SetString(PyExc_TypeError,
781
"string argument without an encoding");
784
encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
787
assert(PyBytes_Check(encoded));
788
new = bytearray_iconcat(self, encoded);
796
/* If it's not unicode, there can't be encoding or errors */
797
if (encoding != NULL || errors != NULL) {
798
PyErr_SetString(PyExc_TypeError,
799
"encoding or errors without a string argument");
804
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
805
if (count == -1 && PyErr_Occurred()) {
806
if (PyErr_ExceptionMatches(PyExc_OverflowError))
810
else if (count < 0) {
811
PyErr_SetString(PyExc_ValueError, "negative count");
816
if (PyByteArray_Resize((PyObject *)self, count))
818
memset(PyByteArray_AS_STRING(self), 0, count);
823
/* Use the buffer API */
824
if (PyObject_CheckBuffer(arg)) {
827
if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
830
if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
831
if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
832
&view, size, 'C') < 0)
834
PyBuffer_Release(&view);
837
PyBuffer_Release(&view);
841
/* XXX Optimize this if the arguments is a list, tuple */
843
/* Get the iterator */
844
it = PyObject_GetIter(arg);
847
iternext = *Py_TYPE(it)->tp_iternext;
849
/* Run the iterator to exhaustion */
854
/* Get the next item */
857
if (PyErr_Occurred()) {
858
if (!PyErr_ExceptionMatches(PyExc_StopIteration))
865
/* Interpret it as an int (__index__) */
866
rc = _getbytevalue(item, &value);
871
/* Append the byte */
872
if (Py_SIZE(self) < self->ob_alloc)
874
else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
876
PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
879
/* Clean up and return success */
884
/* Error handling when it != NULL */
889
/* Mostly copied from string_repr, but without the
890
"smart quote" functionality. */
892
bytearray_repr(PyByteArrayObject *self)
894
const char *quote_prefix = "bytearray(b";
895
const char *quote_postfix = ")";
896
Py_ssize_t length = Py_SIZE(self);
897
/* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
908
if (length > (PY_SSIZE_T_MAX - 15) / 4) {
909
PyErr_SetString(PyExc_OverflowError,
910
"bytearray object is too large to make repr");
914
newsize = 15 + length * 4;
915
buffer = PyObject_Malloc(newsize);
916
if (buffer == NULL) {
921
/* Figure out which quote to use; single is preferred */
923
start = PyByteArray_AS_STRING(self);
924
for (test = start; test < start+length; ++test) {
926
quote = '\''; /* back to single */
929
else if (*test == '\'')
934
while (*quote_prefix)
935
*p++ = *quote_prefix++;
938
bytes = PyByteArray_AS_STRING(self);
939
for (i = 0; i < length; i++) {
940
/* There's at least enough room for a hex escape
941
and a closing quote. */
942
assert(newsize - (p - buffer) >= 5);
944
if (c == '\'' || c == '\\')
945
*p++ = '\\', *p++ = c;
947
*p++ = '\\', *p++ = 't';
949
*p++ = '\\', *p++ = 'n';
951
*p++ = '\\', *p++ = 'r';
953
*p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
954
else if (c < ' ' || c >= 0x7f) {
957
*p++ = Py_hexdigits[(c & 0xf0) >> 4];
958
*p++ = Py_hexdigits[c & 0xf];
963
assert(newsize - (p - buffer) >= 1);
965
while (*quote_postfix) {
966
*p++ = *quote_postfix++;
969
v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
970
PyObject_Free(buffer);
975
bytearray_str(PyObject *op)
977
if (Py_BytesWarningFlag) {
978
if (PyErr_WarnEx(PyExc_BytesWarning,
979
"str() on a bytearray instance", 1))
982
return bytearray_repr((PyByteArrayObject*)op);
986
bytearray_richcompare(PyObject *self, PyObject *other, int op)
988
Py_ssize_t self_size, other_size;
989
Py_buffer self_bytes, other_bytes;
994
/* Bytes can be compared to anything that supports the (binary)
995
buffer API. Except that a comparison with Unicode is always an
996
error, even if the comparison is for equality. */
997
if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
998
PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
999
if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
1000
if (PyErr_WarnEx(PyExc_BytesWarning,
1001
"Comparison between bytearray and string", 1))
1005
Py_RETURN_NOTIMPLEMENTED;
1008
self_size = _getbuffer(self, &self_bytes);
1009
if (self_size < 0) {
1011
Py_RETURN_NOTIMPLEMENTED;
1014
other_size = _getbuffer(other, &other_bytes);
1015
if (other_size < 0) {
1017
PyBuffer_Release(&self_bytes);
1018
Py_RETURN_NOTIMPLEMENTED;
1021
if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1022
/* Shortcut: if the lengths differ, the objects differ */
1023
cmp = (op == Py_NE);
1026
minsize = self_size;
1027
if (other_size < minsize)
1028
minsize = other_size;
1030
cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1031
/* In ISO C, memcmp() guarantees to use unsigned bytes! */
1034
if (self_size < other_size)
1036
else if (self_size > other_size)
1041
case Py_LT: cmp = cmp < 0; break;
1042
case Py_LE: cmp = cmp <= 0; break;
1043
case Py_EQ: cmp = cmp == 0; break;
1044
case Py_NE: cmp = cmp != 0; break;
1045
case Py_GT: cmp = cmp > 0; break;
1046
case Py_GE: cmp = cmp >= 0; break;
1050
res = cmp ? Py_True : Py_False;
1051
PyBuffer_Release(&self_bytes);
1052
PyBuffer_Release(&other_bytes);
1058
bytearray_dealloc(PyByteArrayObject *self)
1060
if (self->ob_exports > 0) {
1061
PyErr_SetString(PyExc_SystemError,
1062
"deallocated bytearray object has exported buffers");
1065
if (self->ob_bytes != 0) {
1066
PyObject_Free(self->ob_bytes);
1068
Py_TYPE(self)->tp_free((PyObject *)self);
1072
/* -------------------------------------------------------------------- */
1075
#define FASTSEARCH fastsearch
1076
#define STRINGLIB(F) stringlib_##F
1077
#define STRINGLIB_CHAR char
1078
#define STRINGLIB_SIZEOF_CHAR 1
1079
#define STRINGLIB_LEN PyByteArray_GET_SIZE
1080
#define STRINGLIB_STR PyByteArray_AS_STRING
1081
#define STRINGLIB_NEW PyByteArray_FromStringAndSize
1082
#define STRINGLIB_ISSPACE Py_ISSPACE
1083
#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
1084
#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1085
#define STRINGLIB_MUTABLE 1
1087
#include "stringlib/fastsearch.h"
1088
#include "stringlib/count.h"
1089
#include "stringlib/find.h"
1090
#include "stringlib/join.h"
1091
#include "stringlib/partition.h"
1092
#include "stringlib/split.h"
1093
#include "stringlib/ctype.h"
1094
#include "stringlib/transmogrify.h"
1097
/* The following Py_LOCAL_INLINE and Py_LOCAL functions
1098
were copied from the old char* style string object. */
1100
/* helper macro to fixup start/end slice values */
1101
#define ADJUST_INDICES(start, end, len) \
1104
else if (end < 0) { \
1115
Py_LOCAL_INLINE(Py_ssize_t)
1116
bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
1123
Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1126
if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex",
1127
args, &subobj, &byte, &start, &end))
1131
if (_getbuffer(subobj, &subbuf) < 0)
1135
sub_len = subbuf.len;
1143
res = stringlib_find_slice(
1144
PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1145
sub, sub_len, start, end);
1147
res = stringlib_rfind_slice(
1148
PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1149
sub, sub_len, start, end);
1152
PyBuffer_Release(&subbuf);
1157
PyDoc_STRVAR(find__doc__,
1158
"B.find(sub[, start[, end]]) -> int\n\
1160
Return the lowest index in B where subsection sub is found,\n\
1161
such that sub is contained within B[start,end]. Optional\n\
1162
arguments start and end are interpreted as in slice notation.\n\
1164
Return -1 on failure.");
1167
bytearray_find(PyByteArrayObject *self, PyObject *args)
1169
Py_ssize_t result = bytearray_find_internal(self, args, +1);
1172
return PyLong_FromSsize_t(result);
1175
PyDoc_STRVAR(count__doc__,
1176
"B.count(sub[, start[, end]]) -> int\n\
1178
Return the number of non-overlapping occurrences of subsection sub in\n\
1179
bytes B[start:end]. Optional arguments start and end are interpreted\n\
1180
as in slice notation.");
1183
bytearray_count(PyByteArrayObject *self, PyObject *args)
1186
const char *str = PyByteArray_AS_STRING(self), *sub;
1189
Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
1192
PyObject *count_obj;
1194
if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte,
1199
if (_getbuffer(sub_obj, &vsub) < 0)
1210
ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self));
1212
count_obj = PyLong_FromSsize_t(
1213
stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
1217
PyBuffer_Release(&vsub);
1222
PyDoc_STRVAR(clear__doc__,
1223
"B.clear() -> None\n\
1225
Remove all items from B.");
1228
bytearray_clear(PyByteArrayObject *self)
1230
if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1235
PyDoc_STRVAR(copy__doc__,
1236
"B.copy() -> bytearray\n\
1238
Return a copy of B.");
1241
bytearray_copy(PyByteArrayObject *self)
1243
return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1244
PyByteArray_GET_SIZE(self));
1247
PyDoc_STRVAR(index__doc__,
1248
"B.index(sub[, start[, end]]) -> int\n\
1250
Like B.find() but raise ValueError when the subsection is not found.");
1253
bytearray_index(PyByteArrayObject *self, PyObject *args)
1255
Py_ssize_t result = bytearray_find_internal(self, args, +1);
1259
PyErr_SetString(PyExc_ValueError,
1260
"subsection not found");
1263
return PyLong_FromSsize_t(result);
1267
PyDoc_STRVAR(rfind__doc__,
1268
"B.rfind(sub[, start[, end]]) -> int\n\
1270
Return the highest index in B where subsection sub is found,\n\
1271
such that sub is contained within B[start,end]. Optional\n\
1272
arguments start and end are interpreted as in slice notation.\n\
1274
Return -1 on failure.");
1277
bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1279
Py_ssize_t result = bytearray_find_internal(self, args, -1);
1282
return PyLong_FromSsize_t(result);
1286
PyDoc_STRVAR(rindex__doc__,
1287
"B.rindex(sub[, start[, end]]) -> int\n\
1289
Like B.rfind() but raise ValueError when the subsection is not found.");
1292
bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1294
Py_ssize_t result = bytearray_find_internal(self, args, -1);
1298
PyErr_SetString(PyExc_ValueError,
1299
"subsection not found");
1302
return PyLong_FromSsize_t(result);
1307
bytearray_contains(PyObject *self, PyObject *arg)
1309
Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1310
if (ival == -1 && PyErr_Occurred()) {
1314
if (_getbuffer(arg, &varg) < 0)
1316
pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1317
varg.buf, varg.len, 0);
1318
PyBuffer_Release(&varg);
1321
if (ival < 0 || ival >= 256) {
1322
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1326
return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL;
1330
/* Matches the end (direction >= 0) or start (direction < 0) of self
1331
* against substr, using the start and end arguments. Returns
1332
* -1 on error, 0 if not found and 1 if found.
1335
_bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
1336
Py_ssize_t end, int direction)
1338
Py_ssize_t len = PyByteArray_GET_SIZE(self);
1343
str = PyByteArray_AS_STRING(self);
1345
if (_getbuffer(substr, &vsubstr) < 0)
1348
ADJUST_INDICES(start, end, len);
1350
if (direction < 0) {
1352
if (start+vsubstr.len > len) {
1357
if (end-start < vsubstr.len || start > len) {
1361
if (end-vsubstr.len > start)
1362
start = end - vsubstr.len;
1364
if (end-start >= vsubstr.len)
1365
rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1368
PyBuffer_Release(&vsubstr);
1373
PyDoc_STRVAR(startswith__doc__,
1374
"B.startswith(prefix[, start[, end]]) -> bool\n\
1376
Return True if B starts with the specified prefix, False otherwise.\n\
1377
With optional start, test B beginning at that position.\n\
1378
With optional end, stop comparing B at that position.\n\
1379
prefix can also be a tuple of bytes to try.");
1382
bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1384
Py_ssize_t start = 0;
1385
Py_ssize_t end = PY_SSIZE_T_MAX;
1389
if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
1391
if (PyTuple_Check(subobj)) {
1393
for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1394
result = _bytearray_tailmatch(self,
1395
PyTuple_GET_ITEM(subobj, i),
1405
result = _bytearray_tailmatch(self, subobj, start, end, -1);
1407
if (PyErr_ExceptionMatches(PyExc_TypeError))
1408
PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes "
1409
"or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
1413
return PyBool_FromLong(result);
1416
PyDoc_STRVAR(endswith__doc__,
1417
"B.endswith(suffix[, start[, end]]) -> bool\n\
1419
Return True if B ends with the specified suffix, False otherwise.\n\
1420
With optional start, test B beginning at that position.\n\
1421
With optional end, stop comparing B at that position.\n\
1422
suffix can also be a tuple of bytes to try.");
1425
bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1427
Py_ssize_t start = 0;
1428
Py_ssize_t end = PY_SSIZE_T_MAX;
1432
if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
1434
if (PyTuple_Check(subobj)) {
1436
for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1437
result = _bytearray_tailmatch(self,
1438
PyTuple_GET_ITEM(subobj, i),
1448
result = _bytearray_tailmatch(self, subobj, start, end, +1);
1450
if (PyErr_ExceptionMatches(PyExc_TypeError))
1451
PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1452
"a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
1456
return PyBool_FromLong(result);
1460
PyDoc_STRVAR(translate__doc__,
1461
"B.translate(table[, deletechars]) -> bytearray\n\
1463
Return a copy of B, where all characters occurring in the\n\
1464
optional argument deletechars are removed, and the remaining\n\
1465
characters have been mapped through the given translation\n\
1466
table, which must be a bytes object of length 256.");
1469
bytearray_translate(PyByteArrayObject *self, PyObject *args)
1471
char *input, *output;
1474
PyObject *input_obj = (PyObject*)self;
1475
const char *output_start;
1477
PyObject *result = NULL;
1478
int trans_table[256];
1479
PyObject *tableobj = NULL, *delobj = NULL;
1480
Py_buffer vtable, vdel;
1482
if (!PyArg_UnpackTuple(args, "translate", 1, 2,
1483
&tableobj, &delobj))
1486
if (tableobj == Py_None) {
1489
} else if (_getbuffer(tableobj, &vtable) < 0) {
1492
if (vtable.len != 256) {
1493
PyErr_SetString(PyExc_ValueError,
1494
"translation table must be 256 characters long");
1495
PyBuffer_Release(&vtable);
1498
table = (const char*)vtable.buf;
1501
if (delobj != NULL) {
1502
if (_getbuffer(delobj, &vdel) < 0) {
1503
if (tableobj != NULL)
1504
PyBuffer_Release(&vtable);
1513
inlen = PyByteArray_GET_SIZE(input_obj);
1514
result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1517
output_start = output = PyByteArray_AsString(result);
1518
input = PyByteArray_AS_STRING(input_obj);
1520
if (vdel.len == 0 && table != NULL) {
1521
/* If no deletions are required, use faster code */
1522
for (i = inlen; --i >= 0; ) {
1523
c = Py_CHARMASK(*input++);
1524
*output++ = table[c];
1529
if (table == NULL) {
1530
for (i = 0; i < 256; i++)
1531
trans_table[i] = Py_CHARMASK(i);
1533
for (i = 0; i < 256; i++)
1534
trans_table[i] = Py_CHARMASK(table[i]);
1537
for (i = 0; i < vdel.len; i++)
1538
trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1540
for (i = inlen; --i >= 0; ) {
1541
c = Py_CHARMASK(*input++);
1542
if (trans_table[c] != -1)
1543
if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1546
/* Fix the size of the resulting string */
1548
if (PyByteArray_Resize(result, output - output_start) < 0) {
1554
if (tableobj != NULL)
1555
PyBuffer_Release(&vtable);
1557
PyBuffer_Release(&vdel);
1563
bytearray_maketrans(PyObject *null, PyObject *args)
1565
return _Py_bytes_maketrans(args);
1569
/* find and count characters and substrings */
1571
#define findchar(target, target_len, c) \
1572
((char *)memchr((const void *)(target), c, target_len))
1575
/* Bytes ops must return a string, create a copy */
1576
Py_LOCAL(PyByteArrayObject *)
1577
return_self(PyByteArrayObject *self)
1579
/* always return a new bytearray */
1580
return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1581
PyByteArray_AS_STRING(self),
1582
PyByteArray_GET_SIZE(self));
1585
Py_LOCAL_INLINE(Py_ssize_t)
1586
countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1589
const char *start=target;
1590
const char *end=target+target_len;
1592
while ( (start=findchar(start, end-start, c)) != NULL ) {
1594
if (count >= maxcount)
1602
/* Algorithms for different cases of string replacement */
1604
/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1605
Py_LOCAL(PyByteArrayObject *)
1606
replace_interleave(PyByteArrayObject *self,
1607
const char *to_s, Py_ssize_t to_len,
1608
Py_ssize_t maxcount)
1610
char *self_s, *result_s;
1611
Py_ssize_t self_len, result_len;
1612
Py_ssize_t count, i;
1613
PyByteArrayObject *result;
1615
self_len = PyByteArray_GET_SIZE(self);
1617
/* 1 at the end plus 1 after every character;
1618
count = min(maxcount, self_len + 1) */
1619
if (maxcount <= self_len)
1622
/* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */
1623
count = self_len + 1;
1625
/* Check for overflow */
1626
/* result_len = count * to_len + self_len; */
1628
if (to_len > (PY_SSIZE_T_MAX - self_len) / count) {
1629
PyErr_SetString(PyExc_OverflowError,
1630
"replace string is too long");
1633
result_len = count * to_len + self_len;
1635
if (! (result = (PyByteArrayObject *)
1636
PyByteArray_FromStringAndSize(NULL, result_len)) )
1639
self_s = PyByteArray_AS_STRING(self);
1640
result_s = PyByteArray_AS_STRING(result);
1642
/* TODO: special case single character, which doesn't need memcpy */
1644
/* Lay the first one down (guaranteed this will occur) */
1645
Py_MEMCPY(result_s, to_s, to_len);
1649
for (i=0; i<count; i++) {
1650
*result_s++ = *self_s++;
1651
Py_MEMCPY(result_s, to_s, to_len);
1655
/* Copy the rest of the original string */
1656
Py_MEMCPY(result_s, self_s, self_len-i);
1661
/* Special case for deleting a single character */
1662
/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1663
Py_LOCAL(PyByteArrayObject *)
1664
replace_delete_single_character(PyByteArrayObject *self,
1665
char from_c, Py_ssize_t maxcount)
1667
char *self_s, *result_s;
1668
char *start, *next, *end;
1669
Py_ssize_t self_len, result_len;
1671
PyByteArrayObject *result;
1673
self_len = PyByteArray_GET_SIZE(self);
1674
self_s = PyByteArray_AS_STRING(self);
1676
count = countchar(self_s, self_len, from_c, maxcount);
1678
return return_self(self);
1681
result_len = self_len - count; /* from_len == 1 */
1682
assert(result_len>=0);
1684
if ( (result = (PyByteArrayObject *)
1685
PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1687
result_s = PyByteArray_AS_STRING(result);
1690
end = self_s + self_len;
1691
while (count-- > 0) {
1692
next = findchar(start, end-start, from_c);
1695
Py_MEMCPY(result_s, start, next-start);
1696
result_s += (next-start);
1699
Py_MEMCPY(result_s, start, end-start);
1704
/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1706
Py_LOCAL(PyByteArrayObject *)
1707
replace_delete_substring(PyByteArrayObject *self,
1708
const char *from_s, Py_ssize_t from_len,
1709
Py_ssize_t maxcount)
1711
char *self_s, *result_s;
1712
char *start, *next, *end;
1713
Py_ssize_t self_len, result_len;
1714
Py_ssize_t count, offset;
1715
PyByteArrayObject *result;
1717
self_len = PyByteArray_GET_SIZE(self);
1718
self_s = PyByteArray_AS_STRING(self);
1720
count = stringlib_count(self_s, self_len,
1726
return return_self(self);
1729
result_len = self_len - (count * from_len);
1730
assert (result_len>=0);
1732
if ( (result = (PyByteArrayObject *)
1733
PyByteArray_FromStringAndSize(NULL, result_len)) == NULL )
1736
result_s = PyByteArray_AS_STRING(result);
1739
end = self_s + self_len;
1740
while (count-- > 0) {
1741
offset = stringlib_find(start, end-start,
1746
next = start + offset;
1748
Py_MEMCPY(result_s, start, next-start);
1750
result_s += (next-start);
1751
start = next+from_len;
1753
Py_MEMCPY(result_s, start, end-start);
1757
/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1758
Py_LOCAL(PyByteArrayObject *)
1759
replace_single_character_in_place(PyByteArrayObject *self,
1760
char from_c, char to_c,
1761
Py_ssize_t maxcount)
1763
char *self_s, *result_s, *start, *end, *next;
1764
Py_ssize_t self_len;
1765
PyByteArrayObject *result;
1767
/* The result string will be the same size */
1768
self_s = PyByteArray_AS_STRING(self);
1769
self_len = PyByteArray_GET_SIZE(self);
1771
next = findchar(self_s, self_len, from_c);
1774
/* No matches; return the original bytes */
1775
return return_self(self);
1778
/* Need to make a new bytes */
1779
result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1782
result_s = PyByteArray_AS_STRING(result);
1783
Py_MEMCPY(result_s, self_s, self_len);
1785
/* change everything in-place, starting with this one */
1786
start = result_s + (next-self_s);
1789
end = result_s + self_len;
1791
while (--maxcount > 0) {
1792
next = findchar(start, end-start, from_c);
1802
/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1803
Py_LOCAL(PyByteArrayObject *)
1804
replace_substring_in_place(PyByteArrayObject *self,
1805
const char *from_s, Py_ssize_t from_len,
1806
const char *to_s, Py_ssize_t to_len,
1807
Py_ssize_t maxcount)
1809
char *result_s, *start, *end;
1811
Py_ssize_t self_len, offset;
1812
PyByteArrayObject *result;
1814
/* The result bytes will be the same size */
1816
self_s = PyByteArray_AS_STRING(self);
1817
self_len = PyByteArray_GET_SIZE(self);
1819
offset = stringlib_find(self_s, self_len,
1823
/* No matches; return the original bytes */
1824
return return_self(self);
1827
/* Need to make a new bytes */
1828
result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1831
result_s = PyByteArray_AS_STRING(result);
1832
Py_MEMCPY(result_s, self_s, self_len);
1834
/* change everything in-place, starting with this one */
1835
start = result_s + offset;
1836
Py_MEMCPY(start, to_s, from_len);
1838
end = result_s + self_len;
1840
while ( --maxcount > 0) {
1841
offset = stringlib_find(start, end-start,
1846
Py_MEMCPY(start+offset, to_s, from_len);
1847
start += offset+from_len;
1853
/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
1854
Py_LOCAL(PyByteArrayObject *)
1855
replace_single_character(PyByteArrayObject *self,
1857
const char *to_s, Py_ssize_t to_len,
1858
Py_ssize_t maxcount)
1860
char *self_s, *result_s;
1861
char *start, *next, *end;
1862
Py_ssize_t self_len, result_len;
1864
PyByteArrayObject *result;
1866
self_s = PyByteArray_AS_STRING(self);
1867
self_len = PyByteArray_GET_SIZE(self);
1869
count = countchar(self_s, self_len, from_c, maxcount);
1871
/* no matches, return unchanged */
1872
return return_self(self);
1875
/* use the difference between current and new, hence the "-1" */
1876
/* result_len = self_len + count * (to_len-1) */
1878
if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) {
1879
PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1882
result_len = self_len + count * (to_len - 1);
1884
if ( (result = (PyByteArrayObject *)
1885
PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1887
result_s = PyByteArray_AS_STRING(result);
1890
end = self_s + self_len;
1891
while (count-- > 0) {
1892
next = findchar(start, end-start, from_c);
1896
if (next == start) {
1897
/* replace with the 'to' */
1898
Py_MEMCPY(result_s, to_s, to_len);
1902
/* copy the unchanged old then the 'to' */
1903
Py_MEMCPY(result_s, start, next-start);
1904
result_s += (next-start);
1905
Py_MEMCPY(result_s, to_s, to_len);
1910
/* Copy the remainder of the remaining bytes */
1911
Py_MEMCPY(result_s, start, end-start);
1916
/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
1917
Py_LOCAL(PyByteArrayObject *)
1918
replace_substring(PyByteArrayObject *self,
1919
const char *from_s, Py_ssize_t from_len,
1920
const char *to_s, Py_ssize_t to_len,
1921
Py_ssize_t maxcount)
1923
char *self_s, *result_s;
1924
char *start, *next, *end;
1925
Py_ssize_t self_len, result_len;
1926
Py_ssize_t count, offset;
1927
PyByteArrayObject *result;
1929
self_s = PyByteArray_AS_STRING(self);
1930
self_len = PyByteArray_GET_SIZE(self);
1932
count = stringlib_count(self_s, self_len,
1937
/* no matches, return unchanged */
1938
return return_self(self);
1941
/* Check for overflow */
1942
/* result_len = self_len + count * (to_len-from_len) */
1944
if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) {
1945
PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1948
result_len = self_len + count * (to_len - from_len);
1950
if ( (result = (PyByteArrayObject *)
1951
PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1953
result_s = PyByteArray_AS_STRING(result);
1956
end = self_s + self_len;
1957
while (count-- > 0) {
1958
offset = stringlib_find(start, end-start,
1963
next = start+offset;
1964
if (next == start) {
1965
/* replace with the 'to' */
1966
Py_MEMCPY(result_s, to_s, to_len);
1970
/* copy the unchanged old then the 'to' */
1971
Py_MEMCPY(result_s, start, next-start);
1972
result_s += (next-start);
1973
Py_MEMCPY(result_s, to_s, to_len);
1975
start = next+from_len;
1978
/* Copy the remainder of the remaining bytes */
1979
Py_MEMCPY(result_s, start, end-start);
1985
Py_LOCAL(PyByteArrayObject *)
1986
replace(PyByteArrayObject *self,
1987
const char *from_s, Py_ssize_t from_len,
1988
const char *to_s, Py_ssize_t to_len,
1989
Py_ssize_t maxcount)
1992
maxcount = PY_SSIZE_T_MAX;
1993
} else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) {
1994
/* nothing to do; return the original bytes */
1995
return return_self(self);
1998
if (maxcount == 0 ||
1999
(from_len == 0 && to_len == 0)) {
2000
/* nothing to do; return the original bytes */
2001
return return_self(self);
2004
/* Handle zero-length special cases */
2006
if (from_len == 0) {
2007
/* insert the 'to' bytes everywhere. */
2008
/* >>> "Python".replace("", ".") */
2009
/* '.P.y.t.h.o.n.' */
2010
return replace_interleave(self, to_s, to_len, maxcount);
2013
/* Except for "".replace("", "A") == "A" there is no way beyond this */
2014
/* point for an empty self bytes to generate a non-empty bytes */
2015
/* Special case so the remaining code always gets a non-empty bytes */
2016
if (PyByteArray_GET_SIZE(self) == 0) {
2017
return return_self(self);
2021
/* delete all occurrences of 'from' bytes */
2022
if (from_len == 1) {
2023
return replace_delete_single_character(
2024
self, from_s[0], maxcount);
2026
return replace_delete_substring(self, from_s, from_len, maxcount);
2030
/* Handle special case where both bytes have the same length */
2032
if (from_len == to_len) {
2033
if (from_len == 1) {
2034
return replace_single_character_in_place(
2040
return replace_substring_in_place(
2041
self, from_s, from_len, to_s, to_len, maxcount);
2045
/* Otherwise use the more generic algorithms */
2046
if (from_len == 1) {
2047
return replace_single_character(self, from_s[0],
2048
to_s, to_len, maxcount);
2050
/* len('from')>=2, len('to')>=1 */
2051
return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
2056
PyDoc_STRVAR(replace__doc__,
2057
"B.replace(old, new[, count]) -> bytearray\n\
2059
Return a copy of B with all occurrences of subsection\n\
2060
old replaced by new. If the optional argument count is\n\
2061
given, only the first count occurrences are replaced.");
2064
bytearray_replace(PyByteArrayObject *self, PyObject *args)
2066
Py_ssize_t count = -1;
2067
PyObject *from, *to, *res;
2068
Py_buffer vfrom, vto;
2070
if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
2073
if (_getbuffer(from, &vfrom) < 0)
2075
if (_getbuffer(to, &vto) < 0) {
2076
PyBuffer_Release(&vfrom);
2080
res = (PyObject *)replace((PyByteArrayObject *) self,
2081
vfrom.buf, vfrom.len,
2082
vto.buf, vto.len, count);
2084
PyBuffer_Release(&vfrom);
2085
PyBuffer_Release(&vto);
2089
PyDoc_STRVAR(split__doc__,
2090
"B.split(sep=None, maxsplit=-1) -> list of bytearrays\n\
2092
Return a list of the sections in B, using sep as the delimiter.\n\
2093
If sep is not given, B is split on ASCII whitespace characters\n\
2094
(space, tab, return, newline, formfeed, vertical tab).\n\
2095
If maxsplit is given, at most maxsplit splits are done.");
2098
bytearray_split(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
2100
static char *kwlist[] = {"sep", "maxsplit", 0};
2101
Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2102
Py_ssize_t maxsplit = -1;
2103
const char *s = PyByteArray_AS_STRING(self), *sub;
2104
PyObject *list, *subobj = Py_None;
2107
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split",
2108
kwlist, &subobj, &maxsplit))
2111
maxsplit = PY_SSIZE_T_MAX;
2113
if (subobj == Py_None)
2114
return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
2116
if (_getbuffer(subobj, &vsub) < 0)
2121
list = stringlib_split(
2122
(PyObject*) self, s, len, sub, n, maxsplit
2124
PyBuffer_Release(&vsub);
2128
PyDoc_STRVAR(partition__doc__,
2129
"B.partition(sep) -> (head, sep, tail)\n\
2131
Search for the separator sep in B, and return the part before it,\n\
2132
the separator itself, and the part after it. If the separator is not\n\
2133
found, returns B and two empty bytearray objects.");
2136
bytearray_partition(PyByteArrayObject *self, PyObject *sep_obj)
2138
PyObject *bytesep, *result;
2140
bytesep = PyByteArray_FromObject(sep_obj);
2144
result = stringlib_partition(
2146
PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2148
PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2155
PyDoc_STRVAR(rpartition__doc__,
2156
"B.rpartition(sep) -> (head, sep, tail)\n\
2158
Search for the separator sep in B, starting at the end of B,\n\
2159
and return the part before it, the separator itself, and the\n\
2160
part after it. If the separator is not found, returns two empty\n\
2161
bytearray objects and B.");
2164
bytearray_rpartition(PyByteArrayObject *self, PyObject *sep_obj)
2166
PyObject *bytesep, *result;
2168
bytesep = PyByteArray_FromObject(sep_obj);
2172
result = stringlib_rpartition(
2174
PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2176
PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2183
PyDoc_STRVAR(rsplit__doc__,
2184
"B.rsplit(sep=None, maxsplit=-1) -> list of bytearrays\n\
2186
Return a list of the sections in B, using sep as the delimiter,\n\
2187
starting at the end of B and working to the front.\n\
2188
If sep is not given, B is split on ASCII whitespace characters\n\
2189
(space, tab, return, newline, formfeed, vertical tab).\n\
2190
If maxsplit is given, at most maxsplit splits are done.");
2193
bytearray_rsplit(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
2195
static char *kwlist[] = {"sep", "maxsplit", 0};
2196
Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2197
Py_ssize_t maxsplit = -1;
2198
const char *s = PyByteArray_AS_STRING(self), *sub;
2199
PyObject *list, *subobj = Py_None;
2202
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit",
2203
kwlist, &subobj, &maxsplit))
2206
maxsplit = PY_SSIZE_T_MAX;
2208
if (subobj == Py_None)
2209
return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
2211
if (_getbuffer(subobj, &vsub) < 0)
2216
list = stringlib_rsplit(
2217
(PyObject*) self, s, len, sub, n, maxsplit
2219
PyBuffer_Release(&vsub);
2223
PyDoc_STRVAR(reverse__doc__,
2224
"B.reverse() -> None\n\
2226
Reverse the order of the values in B in place.");
2228
bytearray_reverse(PyByteArrayObject *self, PyObject *unused)
2230
char swap, *head, *tail;
2231
Py_ssize_t i, j, n = Py_SIZE(self);
2234
head = PyByteArray_AS_STRING(self);
2235
tail = head + n - 1;
2236
for (i = 0; i < j; i++) {
2245
PyDoc_STRVAR(insert__doc__,
2246
"B.insert(index, int) -> None\n\
2248
Insert a single item into the bytearray before the given index.");
2250
bytearray_insert(PyByteArrayObject *self, PyObject *args)
2254
Py_ssize_t where, n = Py_SIZE(self);
2257
if (!PyArg_ParseTuple(args, "nO:insert", &where, &value))
2260
if (n == PY_SSIZE_T_MAX) {
2261
PyErr_SetString(PyExc_OverflowError,
2262
"cannot add more objects to bytearray");
2265
if (!_getbytevalue(value, &ival))
2267
if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2269
buf = PyByteArray_AS_STRING(self);
2278
memmove(buf + where + 1, buf + where, n - where);
2284
PyDoc_STRVAR(append__doc__,
2285
"B.append(int) -> None\n\
2287
Append a single item to the end of B.");
2289
bytearray_append(PyByteArrayObject *self, PyObject *arg)
2292
Py_ssize_t n = Py_SIZE(self);
2294
if (! _getbytevalue(arg, &value))
2296
if (n == PY_SSIZE_T_MAX) {
2297
PyErr_SetString(PyExc_OverflowError,
2298
"cannot add more objects to bytearray");
2301
if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2304
PyByteArray_AS_STRING(self)[n] = value;
2309
PyDoc_STRVAR(extend__doc__,
2310
"B.extend(iterable_of_ints) -> None\n\
2312
Append all the elements from the iterator or sequence to the\n\
2315
bytearray_extend(PyByteArrayObject *self, PyObject *arg)
2317
PyObject *it, *item, *bytearray_obj;
2318
Py_ssize_t buf_size = 0, len = 0;
2322
/* bytearray_setslice code only accepts something supporting PEP 3118. */
2323
if (PyObject_CheckBuffer(arg)) {
2324
if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1)
2330
it = PyObject_GetIter(arg);
2334
/* Try to determine the length of the argument. 32 is arbitrary. */
2335
buf_size = PyObject_LengthHint(arg, 32);
2336
if (buf_size == -1) {
2341
bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
2342
if (bytearray_obj == NULL) {
2346
buf = PyByteArray_AS_STRING(bytearray_obj);
2348
while ((item = PyIter_Next(it)) != NULL) {
2349
if (! _getbytevalue(item, &value)) {
2352
Py_DECREF(bytearray_obj);
2358
if (len >= buf_size) {
2359
buf_size = len + (len >> 1) + 1;
2360
if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
2362
Py_DECREF(bytearray_obj);
2365
/* Recompute the `buf' pointer, since the resizing operation may
2366
have invalidated it. */
2367
buf = PyByteArray_AS_STRING(bytearray_obj);
2372
/* Resize down to exact size. */
2373
if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
2374
Py_DECREF(bytearray_obj);
2378
if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
2379
Py_DECREF(bytearray_obj);
2382
Py_DECREF(bytearray_obj);
2387
PyDoc_STRVAR(pop__doc__,
2388
"B.pop([index]) -> int\n\
2390
Remove and return a single item from B. If no index\n\
2391
argument is given, will pop the last value.");
2393
bytearray_pop(PyByteArrayObject *self, PyObject *args)
2396
Py_ssize_t where = -1, n = Py_SIZE(self);
2399
if (!PyArg_ParseTuple(args, "|n:pop", &where))
2403
PyErr_SetString(PyExc_IndexError,
2404
"pop from empty bytearray");
2408
where += Py_SIZE(self);
2409
if (where < 0 || where >= Py_SIZE(self)) {
2410
PyErr_SetString(PyExc_IndexError, "pop index out of range");
2413
if (!_canresize(self))
2416
buf = PyByteArray_AS_STRING(self);
2418
memmove(buf + where, buf + where + 1, n - where);
2419
if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2422
return PyLong_FromLong((unsigned char)value);
2425
PyDoc_STRVAR(remove__doc__,
2426
"B.remove(int) -> None\n\
2428
Remove the first occurrence of a value in B.");
2430
bytearray_remove(PyByteArrayObject *self, PyObject *arg)
2433
Py_ssize_t where, n = Py_SIZE(self);
2434
char *buf = PyByteArray_AS_STRING(self);
2436
if (! _getbytevalue(arg, &value))
2439
for (where = 0; where < n; where++) {
2440
if (buf[where] == value)
2444
PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
2447
if (!_canresize(self))
2450
memmove(buf + where, buf + where + 1, n - where);
2451
if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2457
/* XXX These two helpers could be optimized if argsize == 1 */
2460
lstrip_helper(char *myptr, Py_ssize_t mysize,
2461
void *argptr, Py_ssize_t argsize)
2464
while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
2470
rstrip_helper(char *myptr, Py_ssize_t mysize,
2471
void *argptr, Py_ssize_t argsize)
2473
Py_ssize_t i = mysize - 1;
2474
while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
2479
PyDoc_STRVAR(strip__doc__,
2480
"B.strip([bytes]) -> bytearray\n\
2482
Strip leading and trailing bytes contained in the argument\n\
2483
and return the result as a new bytearray.\n\
2484
If the argument is omitted, strip ASCII whitespace.");
2486
bytearray_strip(PyByteArrayObject *self, PyObject *args)
2488
Py_ssize_t left, right, mysize, argsize;
2489
char *myptr, *argptr;
2490
PyObject *arg = Py_None;
2492
if (!PyArg_ParseTuple(args, "|O:strip", &arg))
2494
if (arg == Py_None) {
2495
argptr = "\t\n\r\f\v ";
2499
if (_getbuffer(arg, &varg) < 0)
2501
argptr = (char *) varg.buf;
2504
myptr = PyByteArray_AS_STRING(self);
2505
mysize = Py_SIZE(self);
2506
left = lstrip_helper(myptr, mysize, argptr, argsize);
2510
right = rstrip_helper(myptr, mysize, argptr, argsize);
2512
PyBuffer_Release(&varg);
2513
return PyByteArray_FromStringAndSize(myptr + left, right - left);
2516
PyDoc_STRVAR(lstrip__doc__,
2517
"B.lstrip([bytes]) -> bytearray\n\
2519
Strip leading bytes contained in the argument\n\
2520
and return the result as a new bytearray.\n\
2521
If the argument is omitted, strip leading ASCII whitespace.");
2523
bytearray_lstrip(PyByteArrayObject *self, PyObject *args)
2525
Py_ssize_t left, right, mysize, argsize;
2526
char *myptr, *argptr;
2527
PyObject *arg = Py_None;
2529
if (!PyArg_ParseTuple(args, "|O:lstrip", &arg))
2531
if (arg == Py_None) {
2532
argptr = "\t\n\r\f\v ";
2536
if (_getbuffer(arg, &varg) < 0)
2538
argptr = (char *) varg.buf;
2541
myptr = PyByteArray_AS_STRING(self);
2542
mysize = Py_SIZE(self);
2543
left = lstrip_helper(myptr, mysize, argptr, argsize);
2546
PyBuffer_Release(&varg);
2547
return PyByteArray_FromStringAndSize(myptr + left, right - left);
2550
PyDoc_STRVAR(rstrip__doc__,
2551
"B.rstrip([bytes]) -> bytearray\n\
2553
Strip trailing bytes contained in the argument\n\
2554
and return the result as a new bytearray.\n\
2555
If the argument is omitted, strip trailing ASCII whitespace.");
2557
bytearray_rstrip(PyByteArrayObject *self, PyObject *args)
2559
Py_ssize_t right, mysize, argsize;
2560
char *myptr, *argptr;
2561
PyObject *arg = Py_None;
2563
if (!PyArg_ParseTuple(args, "|O:rstrip", &arg))
2565
if (arg == Py_None) {
2566
argptr = "\t\n\r\f\v ";
2570
if (_getbuffer(arg, &varg) < 0)
2572
argptr = (char *) varg.buf;
2575
myptr = PyByteArray_AS_STRING(self);
2576
mysize = Py_SIZE(self);
2577
right = rstrip_helper(myptr, mysize, argptr, argsize);
2579
PyBuffer_Release(&varg);
2580
return PyByteArray_FromStringAndSize(myptr, right);
2583
PyDoc_STRVAR(decode_doc,
2584
"B.decode(encoding='utf-8', errors='strict') -> str\n\
2586
Decode B using the codec registered for encoding. Default encoding\n\
2587
is 'utf-8'. errors may be given to set a different error\n\
2588
handling scheme. Default is 'strict' meaning that encoding errors raise\n\
2589
a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
2590
as well as any other name registered with codecs.register_error that is\n\
2591
able to handle UnicodeDecodeErrors.");
2594
bytearray_decode(PyObject *self, PyObject *args, PyObject *kwargs)
2596
const char *encoding = NULL;
2597
const char *errors = NULL;
2598
static char *kwlist[] = {"encoding", "errors", 0};
2600
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encoding, &errors))
2602
if (encoding == NULL)
2603
encoding = PyUnicode_GetDefaultEncoding();
2604
return PyUnicode_FromEncodedObject(self, encoding, errors);
2607
PyDoc_STRVAR(alloc_doc,
2608
"B.__alloc__() -> int\n\
2610
Return the number of bytes actually allocated.");
2613
bytearray_alloc(PyByteArrayObject *self)
2615
return PyLong_FromSsize_t(self->ob_alloc);
2618
PyDoc_STRVAR(join_doc,
2619
"B.join(iterable_of_bytes) -> bytearray\n\
2621
Concatenate any number of bytes/bytearray objects, with B\n\
2622
in between each pair, and return the result as a new bytearray.");
2625
bytearray_join(PyObject *self, PyObject *iterable)
2627
return stringlib_bytes_join(self, iterable);
2630
PyDoc_STRVAR(splitlines__doc__,
2631
"B.splitlines([keepends]) -> list of lines\n\
2633
Return a list of the lines in B, breaking at line boundaries.\n\
2634
Line breaks are not included in the resulting list unless keepends\n\
2635
is given and true.");
2638
bytearray_splitlines(PyObject *self, PyObject *args, PyObject *kwds)
2640
static char *kwlist[] = {"keepends", 0};
2643
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines",
2647
return stringlib_splitlines(
2648
(PyObject*) self, PyByteArray_AS_STRING(self),
2649
PyByteArray_GET_SIZE(self), keepends
2653
PyDoc_STRVAR(fromhex_doc,
2654
"bytearray.fromhex(string) -> bytearray (static method)\n\
2656
Create a bytearray object from a string of hexadecimal numbers.\n\
2657
Spaces between two numbers are accepted.\n\
2658
Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
2661
hex_digit_to_int(Py_UCS4 c)
2670
if (c >= 'a' && c <= 'f')
2671
return c - 'a' + 10;
2677
bytearray_fromhex(PyObject *cls, PyObject *args)
2679
PyObject *newbytes, *hexobj;
2681
Py_ssize_t hexlen, byteslen, i, j;
2686
if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
2688
assert(PyUnicode_Check(hexobj));
2689
if (PyUnicode_READY(hexobj))
2691
kind = PyUnicode_KIND(hexobj);
2692
data = PyUnicode_DATA(hexobj);
2693
hexlen = PyUnicode_GET_LENGTH(hexobj);
2695
byteslen = hexlen/2; /* This overestimates if there are spaces */
2696
newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
2699
buf = PyByteArray_AS_STRING(newbytes);
2700
for (i = j = 0; i < hexlen; i += 2) {
2701
/* skip over spaces in the input */
2702
while (PyUnicode_READ(kind, data, i) == ' ')
2706
top = hex_digit_to_int(PyUnicode_READ(kind, data, i));
2707
bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1));
2708
if (top == -1 || bot == -1) {
2709
PyErr_Format(PyExc_ValueError,
2710
"non-hexadecimal number found in "
2711
"fromhex() arg at position %zd", i);
2714
buf[j++] = (top << 4) + bot;
2716
if (PyByteArray_Resize(newbytes, j) < 0)
2721
Py_DECREF(newbytes);
2727
_common_reduce(PyByteArrayObject *self, int proto)
2730
_Py_IDENTIFIER(__dict__);
2733
dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
2740
buf = PyByteArray_AS_STRING(self);
2742
/* use str based reduction for backwards compatibility with Python 2.x */
2745
latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
2747
latin1 = PyUnicode_FromString("");
2748
return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2751
/* use more efficient byte based reduction */
2752
if (Py_SIZE(self)) {
2753
return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
2756
return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2761
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2764
bytearray_reduce(PyByteArrayObject *self)
2766
return _common_reduce(self, 2);
2769
PyDoc_STRVAR(reduce_ex_doc, "Return state information for pickling.");
2772
bytearray_reduce_ex(PyByteArrayObject *self, PyObject *args)
2776
if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2779
return _common_reduce(self, proto);
2782
PyDoc_STRVAR(sizeof_doc,
2783
"B.__sizeof__() -> int\n\
2785
Returns the size of B in memory, in bytes");
2787
bytearray_sizeof(PyByteArrayObject *self)
2791
res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
2792
return PyLong_FromSsize_t(res);
2795
static PySequenceMethods bytearray_as_sequence = {
2796
(lenfunc)bytearray_length, /* sq_length */
2797
(binaryfunc)PyByteArray_Concat, /* sq_concat */
2798
(ssizeargfunc)bytearray_repeat, /* sq_repeat */
2799
(ssizeargfunc)bytearray_getitem, /* sq_item */
2801
(ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2802
0, /* sq_ass_slice */
2803
(objobjproc)bytearray_contains, /* sq_contains */
2804
(binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2805
(ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
2808
static PyMappingMethods bytearray_as_mapping = {
2809
(lenfunc)bytearray_length,
2810
(binaryfunc)bytearray_subscript,
2811
(objobjargproc)bytearray_ass_subscript,
2814
static PyBufferProcs bytearray_as_buffer = {
2815
(getbufferproc)bytearray_getbuffer,
2816
(releasebufferproc)bytearray_releasebuffer,
2820
bytearray_methods[] = {
2821
{"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
2822
{"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, reduce_doc},
2823
{"__reduce_ex__", (PyCFunction)bytearray_reduce_ex, METH_VARARGS, reduce_ex_doc},
2824
{"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, sizeof_doc},
2825
{"append", (PyCFunction)bytearray_append, METH_O, append__doc__},
2826
{"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2827
_Py_capitalize__doc__},
2828
{"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
2829
{"clear", (PyCFunction)bytearray_clear, METH_NOARGS, clear__doc__},
2830
{"copy", (PyCFunction)bytearray_copy, METH_NOARGS, copy__doc__},
2831
{"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
2832
{"decode", (PyCFunction)bytearray_decode, METH_VARARGS | METH_KEYWORDS, decode_doc},
2833
{"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__},
2834
{"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
2836
{"extend", (PyCFunction)bytearray_extend, METH_O, extend__doc__},
2837
{"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
2838
{"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS,
2840
{"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
2841
{"insert", (PyCFunction)bytearray_insert, METH_VARARGS, insert__doc__},
2842
{"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2843
_Py_isalnum__doc__},
2844
{"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2845
_Py_isalpha__doc__},
2846
{"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2847
_Py_isdigit__doc__},
2848
{"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2849
_Py_islower__doc__},
2850
{"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2851
_Py_isspace__doc__},
2852
{"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2853
_Py_istitle__doc__},
2854
{"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2855
_Py_isupper__doc__},
2856
{"join", (PyCFunction)bytearray_join, METH_O, join_doc},
2857
{"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
2858
{"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
2859
{"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, lstrip__doc__},
2860
{"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC,
2861
_Py_maketrans__doc__},
2862
{"partition", (PyCFunction)bytearray_partition, METH_O, partition__doc__},
2863
{"pop", (PyCFunction)bytearray_pop, METH_VARARGS, pop__doc__},
2864
{"remove", (PyCFunction)bytearray_remove, METH_O, remove__doc__},
2865
{"replace", (PyCFunction)bytearray_replace, METH_VARARGS, replace__doc__},
2866
{"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, reverse__doc__},
2867
{"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
2868
{"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
2869
{"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
2870
{"rpartition", (PyCFunction)bytearray_rpartition, METH_O, rpartition__doc__},
2871
{"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__},
2872
{"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, rstrip__doc__},
2873
{"split", (PyCFunction)bytearray_split, METH_VARARGS | METH_KEYWORDS, split__doc__},
2874
{"splitlines", (PyCFunction)bytearray_splitlines,
2875
METH_VARARGS | METH_KEYWORDS, splitlines__doc__},
2876
{"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
2878
{"strip", (PyCFunction)bytearray_strip, METH_VARARGS, strip__doc__},
2879
{"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2880
_Py_swapcase__doc__},
2881
{"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
2882
{"translate", (PyCFunction)bytearray_translate, METH_VARARGS,
2884
{"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2885
{"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
2889
PyDoc_STRVAR(bytearray_doc,
2890
"bytearray(iterable_of_ints) -> bytearray\n\
2891
bytearray(string, encoding[, errors]) -> bytearray\n\
2892
bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2893
bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2894
bytearray() -> empty bytes array\n\
2896
Construct an mutable bytearray object from:\n\
2897
- an iterable yielding integers in range(256)\n\
2898
- a text string encoded using the specified encoding\n\
2899
- a bytes or a buffer object\n\
2900
- any object implementing the buffer API.\n\
2904
static PyObject *bytearray_iter(PyObject *seq);
2906
PyTypeObject PyByteArray_Type = {
2907
PyVarObject_HEAD_INIT(&PyType_Type, 0)
2909
sizeof(PyByteArrayObject),
2911
(destructor)bytearray_dealloc, /* tp_dealloc */
2915
0, /* tp_reserved */
2916
(reprfunc)bytearray_repr, /* tp_repr */
2917
0, /* tp_as_number */
2918
&bytearray_as_sequence, /* tp_as_sequence */
2919
&bytearray_as_mapping, /* tp_as_mapping */
2922
bytearray_str, /* tp_str */
2923
PyObject_GenericGetAttr, /* tp_getattro */
2924
0, /* tp_setattro */
2925
&bytearray_as_buffer, /* tp_as_buffer */
2926
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2927
bytearray_doc, /* tp_doc */
2928
0, /* tp_traverse */
2930
(richcmpfunc)bytearray_richcompare, /* tp_richcompare */
2931
0, /* tp_weaklistoffset */
2932
bytearray_iter, /* tp_iter */
2933
0, /* tp_iternext */
2934
bytearray_methods, /* tp_methods */
2939
0, /* tp_descr_get */
2940
0, /* tp_descr_set */
2941
0, /* tp_dictoffset */
2942
(initproc)bytearray_init, /* tp_init */
2943
PyType_GenericAlloc, /* tp_alloc */
2944
PyType_GenericNew, /* tp_new */
2945
PyObject_Del, /* tp_free */
2948
/*********************** Bytes Iterator ****************************/
2952
Py_ssize_t it_index;
2953
PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2957
bytearrayiter_dealloc(bytesiterobject *it)
2959
_PyObject_GC_UNTRACK(it);
2960
Py_XDECREF(it->it_seq);
2961
PyObject_GC_Del(it);
2965
bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
2967
Py_VISIT(it->it_seq);
2972
bytearrayiter_next(bytesiterobject *it)
2974
PyByteArrayObject *seq;
2981
assert(PyByteArray_Check(seq));
2983
if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2984
item = PyLong_FromLong(
2985
(unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
2997
bytearrayiter_length_hint(bytesiterobject *it)
3001
len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
3002
return PyLong_FromSsize_t(len);
3005
PyDoc_STRVAR(length_hint_doc,
3006
"Private method returning an estimate of len(list(it)).");
3009
bytearrayiter_reduce(bytesiterobject *it)
3011
if (it->it_seq != NULL) {
3012
return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
3013
it->it_seq, it->it_index);
3015
PyObject *u = PyUnicode_FromUnicode(NULL, 0);
3018
return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
3023
bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
3025
Py_ssize_t index = PyLong_AsSsize_t(state);
3026
if (index == -1 && PyErr_Occurred())
3030
it->it_index = index;
3034
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3036
static PyMethodDef bytearrayiter_methods[] = {
3037
{"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
3039
{"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
3041
{"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3043
{NULL, NULL} /* sentinel */
3046
PyTypeObject PyByteArrayIter_Type = {
3047
PyVarObject_HEAD_INIT(&PyType_Type, 0)
3048
"bytearray_iterator", /* tp_name */
3049
sizeof(bytesiterobject), /* tp_basicsize */
3050
0, /* tp_itemsize */
3052
(destructor)bytearrayiter_dealloc, /* tp_dealloc */
3056
0, /* tp_reserved */
3058
0, /* tp_as_number */
3059
0, /* tp_as_sequence */
3060
0, /* tp_as_mapping */
3064
PyObject_GenericGetAttr, /* tp_getattro */
3065
0, /* tp_setattro */
3066
0, /* tp_as_buffer */
3067
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3069
(traverseproc)bytearrayiter_traverse, /* tp_traverse */
3071
0, /* tp_richcompare */
3072
0, /* tp_weaklistoffset */
3073
PyObject_SelfIter, /* tp_iter */
3074
(iternextfunc)bytearrayiter_next, /* tp_iternext */
3075
bytearrayiter_methods, /* tp_methods */
3080
bytearray_iter(PyObject *seq)
3082
bytesiterobject *it;
3084
if (!PyByteArray_Check(seq)) {
3085
PyErr_BadInternalCall();
3088
it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3093
it->it_seq = (PyByteArrayObject *)seq;
3094
_PyObject_GC_TRACK(it);
3095
return (PyObject *)it;