Mercurial > cpython-lru_cache-weakref
annotate Objects/bytesobject.c @ 41598:ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 04 May 2007 05:00:04 +0000 |
| parents | cb3c92062ec4 |
| children | bc6ad63cd5f1 |
| rev | line source |
|---|---|
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /* Bytes object implementation */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 /* XXX TO DO: optimizations */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 #define PY_SSIZE_T_CLEAN |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 #include "Python.h" |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
7 #include "structmember.h" |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
9 /* The nullbytes are used by the stringlib during partition. |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
10 * If partition is removed from bytes, nullbytes and its helper |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
11 * Init/Fini should also be removed. |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
12 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
13 static PyBytesObject *nullbytes = NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
14 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
15 void |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
16 PyBytes_Fini(void) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
17 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
18 Py_CLEAR(nullbytes); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
19 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
20 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
21 int |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
22 PyBytes_Init(void) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
23 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
24 nullbytes = PyObject_New(PyBytesObject, &PyBytes_Type); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
25 if (nullbytes == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
26 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
27 nullbytes->ob_bytes = NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
28 nullbytes->ob_size = nullbytes->ob_alloc = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
29 return 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
30 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
31 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
32 /* end nullbytes support */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
33 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
34 /* Helpers */ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
35 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
36 static int |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
37 _getbytevalue(PyObject* arg, int *value) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
38 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
39 PyObject *intarg = PyNumber_Int(arg); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
40 if (! intarg) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
41 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
42 *value = PyInt_AsLong(intarg); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
43 Py_DECREF(intarg); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
44 if (*value < 0 || *value >= 256) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
45 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
46 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
47 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
48 return 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
49 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
50 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
51 Py_ssize_t |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
52 _getbuffer(PyObject *obj, void **ptr) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
53 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
54 PyBufferProcs *buffer = obj->ob_type->tp_as_buffer; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
55 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
56 if (buffer == NULL || |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
57 PyUnicode_Check(obj) || |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
58 buffer->bf_getreadbuffer == NULL || |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
59 buffer->bf_getsegcount == NULL || |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
60 buffer->bf_getsegcount(obj, NULL) != 1) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
61 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
62 *ptr = NULL; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
63 return -1; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
64 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
65 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
66 return buffer->bf_getreadbuffer(obj, 0, ptr); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
67 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
68 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 /* Direct API functions */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
72 PyBytes_FromObject(PyObject *input) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
73 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
74 return PyObject_CallFunctionObjArgs((PyObject *)&PyBytes_Type, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
75 input, NULL); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
76 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
77 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
78 PyObject * |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
79 PyBytes_FromStringAndSize(const char *bytes, Py_ssize_t size) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 PyBytesObject *new; |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
82 int alloc; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
84 assert(size >= 0); |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 new = PyObject_New(PyBytesObject, &PyBytes_Type); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 if (new == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
88 return NULL; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
90 if (size == 0) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
91 new->ob_bytes = NULL; |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
92 alloc = 0; |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
93 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
94 else { |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
95 alloc = size + 1; |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
96 new->ob_bytes = PyMem_Malloc(alloc); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
97 if (new->ob_bytes == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
98 Py_DECREF(new); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
99 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
100 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
101 if (bytes != NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
102 memcpy(new->ob_bytes, bytes, size); |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
103 new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 } |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
105 new->ob_size = size; |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
106 new->ob_alloc = alloc; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
107 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 return (PyObject *)new; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 Py_ssize_t |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 PyBytes_Size(PyObject *self) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 assert(self != NULL); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 assert(PyBytes_Check(self)); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
117 return PyBytes_GET_SIZE(self); |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 char * |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 PyBytes_AsString(PyObject *self) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 assert(self != NULL); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 assert(PyBytes_Check(self)); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
126 return PyBytes_AS_STRING(self); |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 int |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 PyBytes_Resize(PyObject *self, Py_ssize_t size) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 void *sval; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
133 Py_ssize_t alloc = ((PyBytesObject *)self)->ob_alloc; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 assert(self != NULL); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 assert(PyBytes_Check(self)); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 assert(size >= 0); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
139 if (size < alloc / 2) { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
140 /* Major downsize; resize down to exact size */ |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
141 alloc = size + 1; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
142 } |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
143 else if (size < alloc) { |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
144 /* Within allocated size; quick exit */ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
145 ((PyBytesObject *)self)->ob_size = size; |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
146 ((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */ |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
147 return 0; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
148 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
149 else if (size <= alloc * 1.125) { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
150 /* Moderate upsize; overallocate similar to list_resize() */ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
151 alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
152 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
153 else { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
154 /* Major upsize; resize up to exact size */ |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
155 alloc = size + 1; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
156 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
157 |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
158 sval = PyMem_Realloc(((PyBytesObject *)self)->ob_bytes, alloc); |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 if (sval == NULL) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
160 PyErr_NoMemory(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
161 return -1; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
164 ((PyBytesObject *)self)->ob_bytes = sval; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 ((PyBytesObject *)self)->ob_size = size; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
166 ((PyBytesObject *)self)->ob_alloc = alloc; |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
167 ((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */ |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
168 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 return 0; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
172 PyObject * |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
173 PyBytes_Concat(PyObject *a, PyObject *b) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
174 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
175 Py_ssize_t asize, bsize, size; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
176 void *aptr, *bptr; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
177 PyBytesObject *result; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
178 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
179 asize = _getbuffer(a, &aptr); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
180 bsize = _getbuffer(b, &bptr); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
181 if (asize < 0 || bsize < 0) { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
182 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
183 a->ob_type->tp_name, b->ob_type->tp_name); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
184 return NULL; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
185 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
186 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
187 size = asize + bsize; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
188 if (size < 0) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
189 return PyErr_NoMemory(); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
190 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
191 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, size); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
192 if (result != NULL) { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
193 memcpy(result->ob_bytes, aptr, asize); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
194 memcpy(result->ob_bytes + asize, bptr, bsize); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
195 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
196 return (PyObject *)result; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
197 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
198 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 /* Functions stuffed into the type object */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 static Py_ssize_t |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 bytes_length(PyBytesObject *self) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 return self->ob_size; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 static PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
208 bytes_concat(PyBytesObject *self, PyObject *other) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
209 { |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
210 return PyBytes_Concat((PyObject *)self, other); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
211 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
212 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
213 static PyObject * |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
214 bytes_iconcat(PyBytesObject *self, PyObject *other) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
215 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
216 void *optr; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
217 Py_ssize_t osize; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
218 Py_ssize_t mysize; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
219 Py_ssize_t size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
220 |
|
41598
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
221 /* XXX What if other == self? */ |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
222 osize = _getbuffer(other, &optr); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
223 if (osize < 0) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
224 PyErr_Format(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
225 "can't concat bytes to %.100s", other->ob_type->tp_name); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
226 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
227 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
228 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
229 mysize = self->ob_size; |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
230 size = mysize + osize; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
231 if (size < 0) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
232 return PyErr_NoMemory(); |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
233 if (size < self->ob_alloc) { |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
234 self->ob_size = size; |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
235 self->ob_bytes[self->ob_size] = '\0'; /* Trailing null byte */ |
|
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
236 } |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
237 else if (PyBytes_Resize((PyObject *)self, size) < 0) |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
238 return NULL; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
239 memcpy(self->ob_bytes + mysize, optr, osize); |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
240 Py_INCREF(self); |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
241 return (PyObject *)self; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
242 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
243 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
244 static PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
245 bytes_repeat(PyBytesObject *self, Py_ssize_t count) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
246 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
247 PyBytesObject *result; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
248 Py_ssize_t mysize; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
249 Py_ssize_t size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
250 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
251 if (count < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
252 count = 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
253 mysize = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
254 size = mysize * count; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
255 if (count != 0 && size / count != mysize) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
256 return PyErr_NoMemory(); |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
257 result = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, size); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
258 if (result != NULL && size != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
259 if (mysize == 1) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
260 memset(result->ob_bytes, self->ob_bytes[0], size); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
261 else { |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
262 Py_ssize_t i; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
263 for (i = 0; i < count; i++) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
264 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
265 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
266 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
267 return (PyObject *)result; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
268 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
269 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
270 static PyObject * |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
271 bytes_irepeat(PyBytesObject *self, Py_ssize_t count) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
272 { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
273 Py_ssize_t mysize; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
274 Py_ssize_t size; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
275 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
276 if (count < 0) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
277 count = 0; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
278 mysize = self->ob_size; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
279 size = mysize * count; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
280 if (count != 0 && size / count != mysize) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
281 return PyErr_NoMemory(); |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
282 if (size < self->ob_alloc) { |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
283 self->ob_size = size; |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
284 self->ob_bytes[self->ob_size] = '\0'; /* Trailing null byte */ |
|
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
285 } |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
286 else if (PyBytes_Resize((PyObject *)self, size) < 0) |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
287 return NULL; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
288 |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
289 if (mysize == 1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
290 memset(self->ob_bytes, self->ob_bytes[0], size); |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
291 else { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
292 Py_ssize_t i; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
293 for (i = 1; i < count; i++) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
294 memcpy(self->ob_bytes + i*mysize, self->ob_bytes, mysize); |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
295 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
296 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
297 Py_INCREF(self); |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
298 return (PyObject *)self; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
299 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
300 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
301 static int |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
302 bytes_substring(PyBytesObject *self, PyBytesObject *other) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
303 { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
304 Py_ssize_t i; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
305 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
306 if (other->ob_size == 1) { |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
307 return memchr(self->ob_bytes, other->ob_bytes[0], |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
308 self->ob_size) != NULL; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
309 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
310 if (other->ob_size == 0) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
311 return 1; /* Edge case */ |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
312 for (i = 0; i + other->ob_size <= self->ob_size; i++) { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
313 /* XXX Yeah, yeah, lots of optimizations possible... */ |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
314 if (memcmp(self->ob_bytes + i, other->ob_bytes, other->ob_size) == 0) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
315 return 1; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
316 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
317 return 0; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
318 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
319 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
320 static int |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
321 bytes_contains(PyBytesObject *self, PyObject *value) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
322 { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
323 Py_ssize_t ival; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
324 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
325 if (PyBytes_Check(value)) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
326 return bytes_substring(self, (PyBytesObject *)value); |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
327 |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
328 ival = PyNumber_AsSsize_t(value, PyExc_ValueError); |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
329 if (ival == -1 && PyErr_Occurred()) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
330 return -1; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
331 if (ival < 0 || ival >= 256) { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
332 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
333 return -1; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
334 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
335 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
336 return memchr(self->ob_bytes, ival, self->ob_size) != NULL; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
337 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
338 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
339 static PyObject * |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 bytes_getitem(PyBytesObject *self, Py_ssize_t i) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 if (i < 0) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
343 i += self->ob_size; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 if (i < 0 || i >= self->ob_size) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
345 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
346 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
347 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
348 return PyInt_FromLong((unsigned char)(self->ob_bytes[i])); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
349 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
350 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
351 static PyObject * |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
352 bytes_subscript(PyBytesObject *self, PyObject *item) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
353 { |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
354 if (PyIndex_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
355 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
356 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
357 if (i == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
358 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
359 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
360 if (i < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
361 i += PyBytes_GET_SIZE(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
362 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
363 if (i < 0 || i >= self->ob_size) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
364 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
365 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
366 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
367 return PyInt_FromLong((unsigned char)(self->ob_bytes[i])); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
368 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
369 else if (PySlice_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
370 Py_ssize_t start, stop, step, slicelength, cur, i; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
371 if (PySlice_GetIndicesEx((PySliceObject *)item, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
372 PyBytes_GET_SIZE(self), |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
373 &start, &stop, &step, &slicelength) < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
374 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
375 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
376 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
377 if (slicelength <= 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
378 return PyBytes_FromStringAndSize("", 0); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
379 else if (step == 1) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
380 return PyBytes_FromStringAndSize(self->ob_bytes + start, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
381 slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
382 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
383 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
384 char *source_buf = PyBytes_AS_STRING(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
385 char *result_buf = (char *)PyMem_Malloc(slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
386 PyObject *result; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
387 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
388 if (result_buf == NULL) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
389 return PyErr_NoMemory(); |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
390 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
391 for (cur = start, i = 0; i < slicelength; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
392 cur += step, i++) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
393 result_buf[i] = source_buf[cur]; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
394 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
395 result = PyBytes_FromStringAndSize(result_buf, slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
396 PyMem_Free(result_buf); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
397 return result; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
398 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
399 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
400 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
401 PyErr_SetString(PyExc_TypeError, "bytes indices must be integers"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
402 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
403 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
404 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
405 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
406 static int |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
407 bytes_setslice(PyBytesObject *self, Py_ssize_t lo, Py_ssize_t hi, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
408 PyObject *values) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
409 { |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
410 Py_ssize_t avail, needed; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
411 void *bytes; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
412 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
413 if (values == (PyObject *)self) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
414 /* Make a copy an call this function recursively */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
415 int err; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
416 values = PyBytes_FromObject(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
417 if (values == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
418 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
419 err = bytes_setslice(self, lo, hi, values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
420 Py_DECREF(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
421 return err; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
422 } |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
423 if (values == NULL) { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
424 /* del b[lo:hi] */ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
425 bytes = NULL; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
426 needed = 0; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
427 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
428 else { |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
429 needed = _getbuffer(values, &bytes); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
430 if (needed < 0) { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
431 PyErr_Format(PyExc_TypeError, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
432 "can't set bytes slice from %.100s", |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
433 values->ob_type->tp_name); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
434 return -1; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
435 } |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
437 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
438 if (lo < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
439 lo = 0; |
|
39643
3dd61ffd049e
Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents:
39631
diff
changeset
|
440 if (hi < lo) |
|
3dd61ffd049e
Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents:
39631
diff
changeset
|
441 hi = lo; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
442 if (hi > self->ob_size) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
443 hi = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
444 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
445 avail = hi - lo; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
446 if (avail < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
447 lo = hi = avail = 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
448 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
449 if (avail != needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
450 if (avail > needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
451 /* |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
452 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
453 | |<----avail----->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
454 | |<-needed->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
455 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
456 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
457 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
458 self->ob_size - hi); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
459 } |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
460 if (PyBytes_Resize((PyObject *)self, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
461 self->ob_size + needed - avail) < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
462 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
463 if (avail < needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
464 /* |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
465 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
466 | |<-avail->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
467 | |<----needed---->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
468 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
469 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
470 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
471 self->ob_size - lo - needed); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
472 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
473 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
474 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
475 if (needed > 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
476 memcpy(self->ob_bytes + lo, bytes, needed); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
477 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
478 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
479 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
480 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
481 static int |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
482 bytes_setitem(PyBytesObject *self, Py_ssize_t i, PyObject *value) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
483 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
484 Py_ssize_t ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
485 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
486 if (i < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
487 i += self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
488 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
489 if (i < 0 || i >= self->ob_size) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
490 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
491 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
492 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
493 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
494 if (value == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
495 return bytes_setslice(self, i, i+1, NULL); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
496 |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
497 ival = PyNumber_AsSsize_t(value, PyExc_ValueError); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
498 if (ival == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
499 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
500 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
501 if (ival < 0 || ival >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
502 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
503 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
504 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
505 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
506 self->ob_bytes[i] = ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
507 return 0; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
508 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
510 static int |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
511 bytes_ass_subscript(PyBytesObject *self, PyObject *item, PyObject *values) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
512 { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
513 Py_ssize_t start, stop, step, slicelen, needed; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
514 char *bytes; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
515 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
516 if (PyIndex_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
517 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
518 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
519 if (i == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
520 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
521 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
522 if (i < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
523 i += PyBytes_GET_SIZE(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
524 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
525 if (i < 0 || i >= self->ob_size) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
526 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
527 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
528 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
529 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
530 if (values == NULL) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
531 /* Fall through to slice assignment */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
532 start = i; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
533 stop = i + 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
534 step = 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
535 slicelen = 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
536 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
537 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
538 Py_ssize_t ival = PyNumber_AsSsize_t(values, PyExc_ValueError); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
539 if (ival == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
540 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
541 if (ival < 0 || ival >= 256) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
542 PyErr_SetString(PyExc_ValueError, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
543 "byte must be in range(0, 256)"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
544 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
545 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
546 self->ob_bytes[i] = (char)ival; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
547 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
548 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
549 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
550 else if (PySlice_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
551 if (PySlice_GetIndicesEx((PySliceObject *)item, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
552 PyBytes_GET_SIZE(self), |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
553 &start, &stop, &step, &slicelen) < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
554 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
555 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
556 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
557 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
558 PyErr_SetString(PyExc_TypeError, "bytes indices must be integer"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
559 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
560 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
561 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
562 if (values == NULL) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
563 bytes = NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
564 needed = 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
565 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
566 else if (values == (PyObject *)self || !PyBytes_Check(values)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
567 /* Make a copy an call this function recursively */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
568 int err; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
569 values = PyBytes_FromObject(values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
570 if (values == NULL) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
571 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
572 err = bytes_ass_subscript(self, item, values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
573 Py_DECREF(values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
574 return err; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
575 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
576 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
577 assert(PyBytes_Check(values)); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
578 bytes = ((PyBytesObject *)values)->ob_bytes; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
579 needed = ((PyBytesObject *)values)->ob_size; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
580 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
581 /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
582 if ((step < 0 && start < stop) || |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
583 (step > 0 && start > stop)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
584 stop = start; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
585 if (step == 1) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
586 if (slicelen != needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
587 if (slicelen > needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
588 /* |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
589 0 start stop old_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
590 | |<---slicelen--->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
591 | |<-needed->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
592 0 lo new_hi new_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
593 */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
594 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
595 self->ob_size - stop); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
596 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
597 if (PyBytes_Resize((PyObject *)self, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
598 self->ob_size + needed - slicelen) < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
599 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
600 if (slicelen < needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
601 /* |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
602 0 lo hi old_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
603 | |<-avail->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
604 | |<----needed---->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
605 0 lo new_hi new_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
606 */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
607 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
608 self->ob_size - start - needed); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
609 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
610 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
611 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
612 if (needed > 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
613 memcpy(self->ob_bytes + start, bytes, needed); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
614 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
615 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
616 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
617 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
618 if (needed == 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
619 /* Delete slice */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
620 Py_ssize_t cur, i; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
621 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
622 if (step < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
623 stop = start + 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
624 start = stop + step * (slicelen - 1) - 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
625 step = -step; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
626 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
627 for (cur = start, i = 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
628 i < slicelen; cur += step, i++) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
629 Py_ssize_t lim = step - 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
630 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
631 if (cur + step >= PyBytes_GET_SIZE(self)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
632 lim = PyBytes_GET_SIZE(self) - cur - 1; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
633 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
634 memmove(self->ob_bytes + cur - i, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
635 self->ob_bytes + cur + 1, lim); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
636 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
637 /* Move the tail of the bytes, in one chunk */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
638 cur = start + slicelen*step; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
639 if (cur < PyBytes_GET_SIZE(self)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
640 memmove(self->ob_bytes + cur - slicelen, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
641 self->ob_bytes + cur, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
642 PyBytes_GET_SIZE(self) - cur); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
643 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
644 if (PyBytes_Resize((PyObject *)self, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
645 PyBytes_GET_SIZE(self) - slicelen) < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
646 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
647 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
648 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
649 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
650 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
651 /* Assign slice */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
652 Py_ssize_t cur, i; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
653 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
654 if (needed != slicelen) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
655 PyErr_Format(PyExc_ValueError, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
656 "attempt to assign bytes of size %zd " |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
657 "to extended slice of size %zd", |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
658 needed, slicelen); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
659 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
660 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
661 for (cur = start, i = 0; i < slicelen; cur += step, i++) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
662 self->ob_bytes[cur] = bytes[i]; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
663 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
664 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
665 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
666 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
667 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
668 static int |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
669 bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
670 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
671 static char *kwlist[] = {"source", "encoding", "errors", 0}; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
672 PyObject *arg = NULL; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
673 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
674 const char *errors = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
675 Py_ssize_t count; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
676 PyObject *it; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
677 PyObject *(*iternext)(PyObject *); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
678 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
679 if (self->ob_size != 0) { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
680 /* Empty previous contents (yes, do this first of all!) */ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
681 if (PyBytes_Resize((PyObject *)self, 0) < 0) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
682 return -1; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
683 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
684 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
685 /* Parse arguments */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
686 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
687 &arg, &encoding, &errors)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
688 return -1; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
689 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
690 /* Make a quick exit if no first argument */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
691 if (arg == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
692 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
693 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
694 "encoding or errors without sequence argument"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
695 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
696 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
697 return 0; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
698 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
699 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
700 if (PyUnicode_Check(arg)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
701 /* Encode via the codec registry */ |
|
41598
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
702 PyObject *encoded, *new; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
703 if (encoding == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
704 encoding = PyUnicode_GetDefaultEncoding(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
705 encoded = PyCodec_Encode(arg, encoding, errors); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
706 if (encoded == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
707 return -1; |
|
41598
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
708 if (!PyBytes_Check(encoded) && !PyString_Check(encoded)) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
709 PyErr_Format(PyExc_TypeError, |
|
41598
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
710 "encoder did not return a str8 or bytes object (type=%.400s)", |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
711 encoded->ob_type->tp_name); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
712 Py_DECREF(encoded); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
713 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
714 } |
|
41598
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
715 new = bytes_iconcat(self, encoded); |
|
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
716 Py_DECREF(encoded); |
|
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
717 if (new == NULL) |
|
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
718 return -1; |
|
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
719 Py_DECREF(new); |
|
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
720 return 0; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
721 } |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
722 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
723 /* If it's not unicode, there can't be encoding or errors */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
724 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
725 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
726 "encoding or errors without a string argument"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
727 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
728 } |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
729 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
730 /* Is it an int? */ |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
731 count = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
732 if (count == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
733 PyErr_Clear(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
734 else { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
735 if (count < 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
736 PyErr_SetString(PyExc_ValueError, "negative count"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
737 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
738 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
739 if (count > 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
740 if (PyBytes_Resize((PyObject *)self, count)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
741 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
742 memset(self->ob_bytes, 0, count); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
743 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
744 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
745 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
746 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
747 if (PyObject_CheckReadBuffer(arg)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
748 const void *bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
749 Py_ssize_t size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
750 if (PyObject_AsReadBuffer(arg, &bytes, &size) < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
751 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
752 if (PyBytes_Resize((PyObject *)self, size) < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
753 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
754 memcpy(self->ob_bytes, bytes, size); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
755 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
756 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
757 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
758 /* XXX Optimize this if the arguments is a list, tuple */ |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
759 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
760 /* Get the iterator */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
761 it = PyObject_GetIter(arg); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
762 if (it == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
763 return -1; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
764 iternext = *it->ob_type->tp_iternext; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
765 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
766 /* Run the iterator to exhaustion */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
767 for (;;) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
768 PyObject *item; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
769 Py_ssize_t value; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
770 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
771 /* Get the next item */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
772 item = iternext(it); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
773 if (item == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
774 if (PyErr_Occurred()) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
775 if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
776 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
777 PyErr_Clear(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
778 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
779 break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
780 } |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
781 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
782 /* Interpret it as an int (__index__) */ |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
783 value = PyNumber_AsSsize_t(item, PyExc_ValueError); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
784 Py_DECREF(item); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
785 if (value == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
786 goto error; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
787 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
788 /* Range check */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
789 if (value < 0 || value >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
790 PyErr_SetString(PyExc_ValueError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
791 "bytes must be in range(0, 256)"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
792 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
793 } |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
794 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
795 /* Append the byte */ |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
796 if (self->ob_size < self->ob_alloc) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
797 self->ob_size++; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
798 else if (PyBytes_Resize((PyObject *)self, self->ob_size+1) < 0) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
799 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
800 self->ob_bytes[self->ob_size-1] = value; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
801 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
802 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
803 /* Clean up and return success */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
804 Py_DECREF(it); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
805 return 0; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
806 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
807 error: |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
808 /* Error handling when it != NULL */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
809 Py_DECREF(it); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
810 return -1; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
811 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
812 |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
813 /* Mostly copied from string_repr, but without the |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
814 "smart quote" functionality. */ |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
815 static PyObject * |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
816 bytes_repr(PyBytesObject *self) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
817 { |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
818 size_t newsize = 3 + 4 * self->ob_size; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
819 PyObject *v; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
820 if (newsize > PY_SSIZE_T_MAX || newsize / 4 != self->ob_size) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
821 PyErr_SetString(PyExc_OverflowError, |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
822 "bytes object is too large to make repr"); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
823 return NULL; |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
824 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
825 v = PyString_FromStringAndSize((char *)NULL, newsize); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
826 if (v == NULL) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
827 return NULL; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
828 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
829 else { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
830 register Py_ssize_t i; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
831 register char c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
832 register char *p; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
833 int quote = '\''; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
834 |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
835 p = PyString_AS_STRING(v); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
836 *p++ = 'b'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
837 *p++ = quote; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
838 for (i = 0; i < self->ob_size; i++) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
839 /* There's at least enough room for a hex escape |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
840 and a closing quote. */ |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
841 assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
842 c = self->ob_bytes[i]; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
843 if (c == quote || c == '\\') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
844 *p++ = '\\', *p++ = c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
845 else if (c == '\t') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
846 *p++ = '\\', *p++ = 't'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
847 else if (c == '\n') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
848 *p++ = '\\', *p++ = 'n'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
849 else if (c == '\r') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
850 *p++ = '\\', *p++ = 'r'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
851 else if (c == 0) |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
852 *p++ = '\\', *p++ = '0'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
853 else if (c < ' ' || c >= 0x7f) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
854 /* For performance, we don't want to call |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
855 PyOS_snprintf here (extra layers of |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
856 function call). */ |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
857 sprintf(p, "\\x%02x", c & 0xff); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
858 p += 4; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
859 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
860 else |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
861 *p++ = c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
862 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
863 assert(newsize - (p - PyString_AS_STRING(v)) >= 1); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
864 *p++ = quote; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
865 *p = '\0'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
866 _PyString_Resize( |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
867 &v, (p - PyString_AS_STRING(v))); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
868 return v; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
869 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
870 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
871 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
872 static PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
873 bytes_str(PyBytesObject *self) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
874 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
875 return PyString_FromStringAndSize(self->ob_bytes, self->ob_size); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
876 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
877 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
878 static PyObject * |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
879 bytes_richcompare(PyObject *self, PyObject *other, int op) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
880 { |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
881 Py_ssize_t self_size, other_size; |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
882 void *self_bytes, *other_bytes; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
883 PyObject *res; |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
884 Py_ssize_t minsize; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
885 int cmp; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
886 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
887 /* Bytes can be compared to anything that supports the (binary) buffer |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
888 API. Except Unicode. */ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
889 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
890 self_size = _getbuffer(self, &self_bytes); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
891 if (self_size < 0) { |
|
41412
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
892 Py_INCREF(Py_NotImplemented); |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
893 return Py_NotImplemented; |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
894 } |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
895 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
896 other_size = _getbuffer(other, &other_bytes); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
897 if (other_size < 0) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
898 Py_INCREF(Py_NotImplemented); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
899 return Py_NotImplemented; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
900 } |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
901 |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
902 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
903 /* Shortcut: if the lengths differ, the objects differ */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
904 cmp = (op == Py_NE); |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
905 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
906 else { |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
907 minsize = self_size; |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
908 if (other_size < minsize) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
909 minsize = other_size; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
910 |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
911 cmp = memcmp(self_bytes, other_bytes, minsize); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
912 /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
913 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
914 if (cmp == 0) { |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
915 if (self_size < other_size) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
916 cmp = -1; |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
917 else if (self_size > other_size) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
918 cmp = 1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
919 } |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
920 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
921 switch (op) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
922 case Py_LT: cmp = cmp < 0; break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
923 case Py_LE: cmp = cmp <= 0; break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
924 case Py_EQ: cmp = cmp == 0; break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
925 case Py_NE: cmp = cmp != 0; break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
926 case Py_GT: cmp = cmp > 0; break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
927 case Py_GE: cmp = cmp >= 0; break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
928 } |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
929 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
930 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
931 res = cmp ? Py_True : Py_False; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
932 Py_INCREF(res); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
933 return res; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
934 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
935 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
936 static void |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
937 bytes_dealloc(PyBytesObject *self) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
938 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
939 if (self->ob_bytes != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
940 PyMem_Free(self->ob_bytes); |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
941 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
942 self->ob_type->tp_free((PyObject *)self); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
943 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
944 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
945 static Py_ssize_t |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
946 bytes_getbuffer(PyBytesObject *self, Py_ssize_t index, const void **ptr) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
947 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
948 if (index != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
949 PyErr_SetString(PyExc_SystemError, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
950 "accessing non-existent bytes segment"); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
951 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
952 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
953 *ptr = (void *)self->ob_bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
954 return self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
955 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
956 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
957 static Py_ssize_t |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
958 bytes_getsegcount(PyStringObject *self, Py_ssize_t *lenp) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
959 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
960 if (lenp) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
961 *lenp = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
962 return 1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
963 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
964 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
965 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
966 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
967 /* -------------------------------------------------------------------- */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
968 /* Methods */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
969 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
970 #define STRINGLIB_CHAR char |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
971 #define STRINGLIB_CMP memcmp |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
972 #define STRINGLIB_LEN PyBytes_GET_SIZE |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
973 #define STRINGLIB_NEW PyBytes_FromStringAndSize |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
974 #define STRINGLIB_EMPTY nullbytes |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
975 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
976 #include "stringlib/fastsearch.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
977 #include "stringlib/count.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
978 #include "stringlib/find.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
979 #include "stringlib/partition.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
980 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
981 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
982 /* The following Py_LOCAL_INLINE and Py_LOCAL functions |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
983 were copied from the old char* style string object. */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
984 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
985 Py_LOCAL_INLINE(void) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
986 _adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
987 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
988 if (*end > len) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
989 *end = len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
990 else if (*end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
991 *end += len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
992 if (*end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
993 *end = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
994 if (*start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
995 *start += len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
996 if (*start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
997 *start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
998 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
999 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1000 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1001 Py_LOCAL_INLINE(Py_ssize_t) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1002 bytes_find_internal(PyBytesObject *self, PyObject *args, int dir) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1003 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1004 PyObject *subobj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1005 const char *sub; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1006 Py_ssize_t sub_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1007 Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1008 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1009 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1010 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1011 return -2; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1012 if (PyBytes_Check(subobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1013 sub = PyBytes_AS_STRING(subobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1014 sub_len = PyBytes_GET_SIZE(subobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1015 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1016 else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1017 /* XXX - the "expected a character buffer object" is pretty |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1018 confusing for a non-expert. remap to something else ? */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1019 return -2; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1020 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1021 if (dir > 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1022 return stringlib_find_slice( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1023 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1024 sub, sub_len, start, end); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1025 else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1026 return stringlib_rfind_slice( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1027 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1028 sub, sub_len, start, end); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1029 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1030 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1031 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1032 PyDoc_STRVAR(find__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1033 "B.find(sub [,start [,end]]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1034 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1035 Return the lowest index in B where subsection sub is found,\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1036 such that sub is contained within s[start,end]. Optional\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1037 arguments start and end are interpreted as in slice notation.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1038 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1039 Return -1 on failure."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1040 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1041 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1042 bytes_find(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1043 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1044 Py_ssize_t result = bytes_find_internal(self, args, +1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1045 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1046 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1047 return PyInt_FromSsize_t(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1048 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1049 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1050 PyDoc_STRVAR(count__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1051 "B.count(sub[, start[, end]]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1052 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1053 Return the number of non-overlapping occurrences of subsection sub in\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1054 bytes B[start:end]. Optional arguments start and end are interpreted\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1055 as in slice notation."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1056 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1057 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1058 bytes_count(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1059 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1060 PyObject *sub_obj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1061 const char *str = PyBytes_AS_STRING(self), *sub; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1062 Py_ssize_t sub_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1063 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1064 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1065 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1066 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1067 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1068 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1069 if (PyBytes_Check(sub_obj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1070 sub = PyBytes_AS_STRING(sub_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1071 sub_len = PyBytes_GET_SIZE(sub_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1072 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1073 else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1074 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1075 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1076 _adjust_indices(&start, &end, PyString_GET_SIZE(self)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1077 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1078 return PyInt_FromSsize_t( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1079 stringlib_count(str + start, end - start, sub, sub_len) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1080 ); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1081 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1082 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1083 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1084 PyDoc_STRVAR(index__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1085 "B.index(sub [,start [,end]]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1086 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1087 Like B.find() but raise ValueError when the subsection is not found."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1088 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1089 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1090 bytes_index(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1091 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1092 Py_ssize_t result = bytes_find_internal(self, args, +1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1093 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1094 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1095 if (result == -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1096 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1097 "subsection not found"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1098 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1099 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1100 return PyInt_FromSsize_t(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1101 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1102 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1103 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1104 PyDoc_STRVAR(rfind__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1105 "B.rfind(sub [,start [,end]]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1106 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1107 Return the highest index in B where subsection sub is found,\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1108 such that sub is contained within s[start,end]. Optional\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1109 arguments start and end are interpreted as in slice notation.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1110 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1111 Return -1 on failure."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1112 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1113 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1114 bytes_rfind(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1115 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1116 Py_ssize_t result = bytes_find_internal(self, args, -1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1117 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1118 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1119 return PyInt_FromSsize_t(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1120 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1121 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1122 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1123 PyDoc_STRVAR(rindex__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1124 "B.rindex(sub [,start [,end]]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1125 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1126 Like B.rfind() but raise ValueError when the subsection is not found."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1127 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1128 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1129 bytes_rindex(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1130 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1131 Py_ssize_t result = bytes_find_internal(self, args, -1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1132 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1133 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1134 if (result == -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1135 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1136 "subsection not found"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1137 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1138 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1139 return PyInt_FromSsize_t(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1140 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1141 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1142 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1143 /* Matches the end (direction >= 0) or start (direction < 0) of self |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1144 * against substr, using the start and end arguments. Returns |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1145 * -1 on error, 0 if not found and 1 if found. |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1146 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1147 Py_LOCAL(int) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1148 _bytes_tailmatch(PyBytesObject *self, PyObject *substr, Py_ssize_t start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1149 Py_ssize_t end, int direction) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1150 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1151 Py_ssize_t len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1152 Py_ssize_t slen; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1153 const char* sub; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1154 const char* str; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1155 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1156 if (PyBytes_Check(substr)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1157 sub = PyBytes_AS_STRING(substr); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1158 slen = PyBytes_GET_SIZE(substr); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1159 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1160 else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1161 return -1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1162 str = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1163 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1164 _adjust_indices(&start, &end, len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1165 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1166 if (direction < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1167 /* startswith */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1168 if (start+slen > len) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1169 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1170 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1171 /* endswith */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1172 if (end-start < slen || start > len) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1173 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1174 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1175 if (end-slen > start) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1176 start = end - slen; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1177 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1178 if (end-start >= slen) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1179 return ! memcmp(str+start, sub, slen); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1180 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1181 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1182 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1183 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1184 PyDoc_STRVAR(startswith__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1185 "B.startswith(prefix[, start[, end]]) -> bool\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1186 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1187 Return True if B starts with the specified prefix, False otherwise.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1188 With optional start, test B beginning at that position.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1189 With optional end, stop comparing B at that position.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1190 prefix can also be a tuple of strings to try."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1191 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1192 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1193 bytes_startswith(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1194 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1195 Py_ssize_t start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1196 Py_ssize_t end = PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1197 PyObject *subobj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1198 int result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1199 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1200 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1201 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1202 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1203 if (PyTuple_Check(subobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1204 Py_ssize_t i; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1205 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1206 result = _bytes_tailmatch(self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1207 PyTuple_GET_ITEM(subobj, i), |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1208 start, end, -1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1209 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1210 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1211 else if (result) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1212 Py_RETURN_TRUE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1213 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1214 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1215 Py_RETURN_FALSE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1216 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1217 result = _bytes_tailmatch(self, subobj, start, end, -1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1218 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1219 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1220 else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1221 return PyBool_FromLong(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1222 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1223 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1224 PyDoc_STRVAR(endswith__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1225 "B.endswith(suffix[, start[, end]]) -> bool\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1226 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1227 Return True if B ends with the specified suffix, False otherwise.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1228 With optional start, test B beginning at that position.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1229 With optional end, stop comparing B at that position.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1230 suffix can also be a tuple of strings to try."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1231 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1232 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1233 bytes_endswith(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1234 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1235 Py_ssize_t start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1236 Py_ssize_t end = PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1237 PyObject *subobj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1238 int result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1239 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1240 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1241 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1242 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1243 if (PyTuple_Check(subobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1244 Py_ssize_t i; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1245 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1246 result = _bytes_tailmatch(self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1247 PyTuple_GET_ITEM(subobj, i), |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1248 start, end, +1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1249 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1250 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1251 else if (result) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1252 Py_RETURN_TRUE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1253 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1254 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1255 Py_RETURN_FALSE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1256 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1257 result = _bytes_tailmatch(self, subobj, start, end, +1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1258 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1259 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1260 else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1261 return PyBool_FromLong(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1262 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1263 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1264 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1265 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1266 PyDoc_STRVAR(translate__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1267 "B.translate(table [,deletechars]) -> bytes\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1268 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1269 Return a copy of the bytes B, where all characters occurring\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1270 in the optional argument deletechars are removed, and the\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1271 remaining characters have been mapped through the given\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1272 translation table, which must be a bytes of length 256."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1273 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1274 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1275 bytes_translate(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1276 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1277 register char *input, *output; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1278 register const char *table; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1279 register Py_ssize_t i, c, changed = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1280 PyObject *input_obj = (PyObject*)self; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1281 const char *table1, *output_start, *del_table=NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1282 Py_ssize_t inlen, tablen, dellen = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1283 PyObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1284 int trans_table[256]; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1285 PyObject *tableobj, *delobj = NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1286 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1287 if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1288 &tableobj, &delobj)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1289 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1290 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1291 if (PyBytes_Check(tableobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1292 table1 = PyBytes_AS_STRING(tableobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1293 tablen = PyBytes_GET_SIZE(tableobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1294 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1295 else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1296 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1297 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1298 if (tablen != 256) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1299 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1300 "translation table must be 256 characters long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1301 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1302 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1303 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1304 if (delobj != NULL) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1305 if (PyBytes_Check(delobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1306 del_table = PyBytes_AS_STRING(delobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1307 dellen = PyBytes_GET_SIZE(delobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1308 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1309 else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1310 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1311 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1312 else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1313 del_table = NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1314 dellen = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1315 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1316 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1317 table = table1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1318 inlen = PyBytes_GET_SIZE(input_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1319 result = PyBytes_FromStringAndSize((char *)NULL, inlen); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1320 if (result == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1321 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1322 output_start = output = PyBytes_AsString(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1323 input = PyBytes_AS_STRING(input_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1324 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1325 if (dellen == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1326 /* If no deletions are required, use faster code */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1327 for (i = inlen; --i >= 0; ) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1328 c = Py_CHARMASK(*input++); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1329 if (Py_CHARMASK((*output++ = table[c])) != c) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1330 changed = 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1331 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1332 if (changed || !PyBytes_CheckExact(input_obj)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1333 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1334 Py_DECREF(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1335 Py_INCREF(input_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1336 return input_obj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1337 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1338 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1339 for (i = 0; i < 256; i++) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1340 trans_table[i] = Py_CHARMASK(table[i]); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1341 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1342 for (i = 0; i < dellen; i++) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1343 trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1344 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1345 for (i = inlen; --i >= 0; ) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1346 c = Py_CHARMASK(*input++); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1347 if (trans_table[c] != -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1348 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1349 continue; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1350 changed = 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1351 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1352 if (!changed && PyBytes_CheckExact(input_obj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1353 Py_DECREF(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1354 Py_INCREF(input_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1355 return input_obj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1356 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1357 /* Fix the size of the resulting string */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1358 if (inlen > 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1359 PyBytes_Resize(result, output - output_start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1360 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1361 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1362 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1363 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1364 #define FORWARD 1 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1365 #define REVERSE -1 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1366 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1367 /* find and count characters and substrings */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1368 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1369 #define findchar(target, target_len, c) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1370 ((char *)memchr((const void *)(target), c, target_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1371 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1372 /* Don't call if length < 2 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1373 #define Py_STRING_MATCH(target, offset, pattern, length) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1374 (target[offset] == pattern[0] && \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1375 target[offset+length-1] == pattern[length-1] && \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1376 !memcmp(target+offset+1, pattern+1, length-2) ) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1377 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1378 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1379 /* Bytes ops must return a string. */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1380 /* If the object is subclass of bytes, create a copy */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1381 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1382 return_self(PyBytesObject *self) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1383 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1384 if (PyBytes_CheckExact(self)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1385 Py_INCREF(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1386 return (PyBytesObject *)self; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1387 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1388 return (PyBytesObject *)PyBytes_FromStringAndSize( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1389 PyBytes_AS_STRING(self), |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1390 PyBytes_GET_SIZE(self)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1391 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1392 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1393 Py_LOCAL_INLINE(Py_ssize_t) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1394 countchar(const char *target, int target_len, char c, Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1395 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1396 Py_ssize_t count=0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1397 const char *start=target; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1398 const char *end=target+target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1399 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1400 while ( (start=findchar(start, end-start, c)) != NULL ) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1401 count++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1402 if (count >= maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1403 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1404 start += 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1405 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1406 return count; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1407 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1408 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1409 Py_LOCAL(Py_ssize_t) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1410 findstring(const char *target, Py_ssize_t target_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1411 const char *pattern, Py_ssize_t pattern_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1412 Py_ssize_t start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1413 Py_ssize_t end, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1414 int direction) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1415 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1416 if (start < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1417 start += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1418 if (start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1419 start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1420 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1421 if (end > target_len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1422 end = target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1423 } else if (end < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1424 end += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1425 if (end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1426 end = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1427 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1428 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1429 /* zero-length substrings always match at the first attempt */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1430 if (pattern_len == 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1431 return (direction > 0) ? start : end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1432 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1433 end -= pattern_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1434 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1435 if (direction < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1436 for (; end >= start; end--) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1437 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1438 return end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1439 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1440 for (; start <= end; start++) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1441 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1442 return start; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1443 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1444 return -1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1445 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1446 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1447 Py_LOCAL_INLINE(Py_ssize_t) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1448 countstring(const char *target, Py_ssize_t target_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1449 const char *pattern, Py_ssize_t pattern_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1450 Py_ssize_t start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1451 Py_ssize_t end, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1452 int direction, Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1453 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1454 Py_ssize_t count=0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1455 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1456 if (start < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1457 start += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1458 if (start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1459 start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1460 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1461 if (end > target_len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1462 end = target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1463 } else if (end < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1464 end += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1465 if (end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1466 end = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1467 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1468 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1469 /* zero-length substrings match everywhere */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1470 if (pattern_len == 0 || maxcount == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1471 if (target_len+1 < maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1472 return target_len+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1473 return maxcount; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1474 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1475 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1476 end -= pattern_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1477 if (direction < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1478 for (; (end >= start); end--) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1479 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1480 count++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1481 if (--maxcount <= 0) break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1482 end -= pattern_len-1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1483 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1484 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1485 for (; (start <= end); start++) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1486 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1487 count++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1488 if (--maxcount <= 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1489 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1490 start += pattern_len-1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1491 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1492 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1493 return count; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1494 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1495 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1496 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1497 /* Algorithms for different cases of string replacement */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1498 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1499 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1500 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1501 replace_interleave(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1502 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1503 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1504 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1505 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1506 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1507 Py_ssize_t count, i, product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1508 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1509 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1510 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1511 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1512 /* 1 at the end plus 1 after every character */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1513 count = self_len+1; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
1514 if (maxcount < count) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1515 count = maxcount; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1516 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1517 /* Check for overflow */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1518 /* result_len = count * to_len + self_len; */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1519 product = count * to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1520 if (product / to_len != count) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1521 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1522 "replace string is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1523 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1524 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1525 result_len = product + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1526 if (result_len < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1527 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1528 "replace string is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1529 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1530 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1531 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1532 if (! (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1533 PyBytes_FromStringAndSize(NULL, result_len)) ) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1534 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1535 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1536 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1537 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1538 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1539 /* TODO: special case single character, which doesn't need memcpy */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1540 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1541 /* Lay the first one down (guaranteed this will occur) */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1542 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1543 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1544 count -= 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1545 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1546 for (i=0; i<count; i++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1547 *result_s++ = *self_s++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1548 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1549 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1550 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1551 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1552 /* Copy the rest of the original string */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1553 Py_MEMCPY(result_s, self_s, self_len-i); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1554 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1555 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1556 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1557 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1558 /* Special case for deleting a single character */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1559 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1560 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1561 replace_delete_single_character(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1562 char from_c, Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1563 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1564 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1565 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1566 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1567 Py_ssize_t count; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1568 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1569 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1570 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1571 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1572 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1573 count = countchar(self_s, self_len, from_c, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1574 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1575 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1576 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1577 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1578 result_len = self_len - count; /* from_len == 1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1579 assert(result_len>=0); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1580 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1581 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1582 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1583 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1584 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1585 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1586 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1587 end = self_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1588 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1589 next = findchar(start, end-start, from_c); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1590 if (next == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1591 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1592 Py_MEMCPY(result_s, start, next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1593 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1594 start = next+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1595 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1596 Py_MEMCPY(result_s, start, end-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1597 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1598 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1599 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1600 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1601 /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1602 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1603 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1604 replace_delete_substring(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1605 const char *from_s, Py_ssize_t from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1606 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1607 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1608 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1609 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1610 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1611 Py_ssize_t count, offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1612 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1613 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1614 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1615 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1616 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1617 count = countstring(self_s, self_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1618 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1619 0, self_len, 1, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1620 maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1621 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1622 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1623 /* no matches */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1624 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1625 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1626 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1627 result_len = self_len - (count * from_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1628 assert (result_len>=0); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1629 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1630 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1631 PyBytes_FromStringAndSize(NULL, result_len)) == NULL ) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1632 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1633 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1634 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1635 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1636 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1637 end = self_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1638 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1639 offset = findstring(start, end-start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1640 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1641 0, end-start, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1642 if (offset == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1643 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1644 next = start + offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1645 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1646 Py_MEMCPY(result_s, start, next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1647 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1648 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1649 start = next+from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1650 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1651 Py_MEMCPY(result_s, start, end-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1652 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1653 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1654 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1655 /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1656 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1657 replace_single_character_in_place(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1658 char from_c, char to_c, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1659 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1660 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1661 char *self_s, *result_s, *start, *end, *next; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1662 Py_ssize_t self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1663 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1664 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1665 /* The result string will be the same size */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1666 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1667 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1668 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1669 next = findchar(self_s, self_len, from_c); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1670 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1671 if (next == NULL) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1672 /* No matches; return the original bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1673 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1674 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1675 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1676 /* Need to make a new bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1677 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1678 if (result == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1679 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1680 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1681 Py_MEMCPY(result_s, self_s, self_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1682 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1683 /* change everything in-place, starting with this one */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1684 start = result_s + (next-self_s); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1685 *start = to_c; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1686 start++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1687 end = result_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1688 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1689 while (--maxcount > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1690 next = findchar(start, end-start, from_c); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1691 if (next == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1692 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1693 *next = to_c; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1694 start = next+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1695 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1696 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1697 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1698 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1699 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1700 /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1701 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1702 replace_substring_in_place(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1703 const char *from_s, Py_ssize_t from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1704 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1705 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1706 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1707 char *result_s, *start, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1708 char *self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1709 Py_ssize_t self_len, offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1710 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1711 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1712 /* The result bytes will be the same size */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1713 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1714 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1715 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1716 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1717 offset = findstring(self_s, self_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1718 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1719 0, self_len, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1720 if (offset == -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1721 /* No matches; return the original bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1722 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1723 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1724 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1725 /* Need to make a new bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1726 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1727 if (result == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1728 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1729 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1730 Py_MEMCPY(result_s, self_s, self_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1731 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1732 /* change everything in-place, starting with this one */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1733 start = result_s + offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1734 Py_MEMCPY(start, to_s, from_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1735 start += from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1736 end = result_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1737 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1738 while ( --maxcount > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1739 offset = findstring(start, end-start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1740 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1741 0, end-start, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1742 if (offset==-1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1743 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1744 Py_MEMCPY(start+offset, to_s, from_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1745 start += offset+from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1746 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1747 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1748 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1749 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1750 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1751 /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1752 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1753 replace_single_character(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1754 char from_c, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1755 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1756 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1757 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1758 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1759 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1760 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1761 Py_ssize_t count, product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1762 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1763 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1764 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1765 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1766 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1767 count = countchar(self_s, self_len, from_c, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1768 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1769 /* no matches, return unchanged */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1770 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1771 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1772 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1773 /* use the difference between current and new, hence the "-1" */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1774 /* result_len = self_len + count * (to_len-1) */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1775 product = count * (to_len-1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1776 if (product / (to_len-1) != count) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1777 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1778 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1779 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1780 result_len = self_len + product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1781 if (result_len < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1782 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1783 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1784 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1785 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1786 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1787 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1788 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1789 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1790 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1791 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1792 end = self_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1793 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1794 next = findchar(start, end-start, from_c); |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
1795 if (next == NULL) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1796 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1797 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1798 if (next == start) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1799 /* replace with the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1800 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1801 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1802 start += 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1803 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1804 /* copy the unchanged old then the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1805 Py_MEMCPY(result_s, start, next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1806 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1807 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1808 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1809 start = next+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1810 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1811 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1812 /* Copy the remainder of the remaining bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1813 Py_MEMCPY(result_s, start, end-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1814 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1815 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1816 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1817 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1818 /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1819 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1820 replace_substring(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1821 const char *from_s, Py_ssize_t from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1822 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1823 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1824 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1825 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1826 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1827 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1828 Py_ssize_t count, offset, product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1829 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1830 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1831 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1832 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1833 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1834 count = countstring(self_s, self_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1835 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1836 0, self_len, FORWARD, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1837 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1838 /* no matches, return unchanged */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1839 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1840 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1841 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1842 /* Check for overflow */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1843 /* result_len = self_len + count * (to_len-from_len) */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1844 product = count * (to_len-from_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1845 if (product / (to_len-from_len) != count) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1846 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1847 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1848 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1849 result_len = self_len + product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1850 if (result_len < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1851 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1852 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1853 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1854 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1855 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1856 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1857 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1858 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1859 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1860 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1861 end = self_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1862 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1863 offset = findstring(start, end-start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1864 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1865 0, end-start, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1866 if (offset == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1867 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1868 next = start+offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1869 if (next == start) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1870 /* replace with the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1871 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1872 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1873 start += from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1874 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1875 /* copy the unchanged old then the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1876 Py_MEMCPY(result_s, start, next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1877 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1878 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1879 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1880 start = next+from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1881 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1882 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1883 /* Copy the remainder of the remaining bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1884 Py_MEMCPY(result_s, start, end-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1885 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1886 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1887 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1888 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1889 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1890 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1891 replace(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1892 const char *from_s, Py_ssize_t from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1893 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1894 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1895 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1896 if (maxcount < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1897 maxcount = PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1898 } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1899 /* nothing to do; return the original bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1900 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1901 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1902 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1903 if (maxcount == 0 || |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1904 (from_len == 0 && to_len == 0)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1905 /* nothing to do; return the original bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1906 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1907 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1908 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1909 /* Handle zero-length special cases */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1910 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1911 if (from_len == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1912 /* insert the 'to' bytes everywhere. */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1913 /* >>> "Python".replace("", ".") */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1914 /* '.P.y.t.h.o.n.' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1915 return replace_interleave(self, to_s, to_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1916 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1917 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1918 /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1919 /* point for an empty self bytes to generate a non-empty bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1920 /* Special case so the remaining code always gets a non-empty bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1921 if (PyBytes_GET_SIZE(self) == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1922 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1923 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1924 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1925 if (to_len == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1926 /* delete all occurances of 'from' bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1927 if (from_len == 1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1928 return replace_delete_single_character( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1929 self, from_s[0], maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1930 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1931 return replace_delete_substring(self, from_s, from_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1932 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1933 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1934 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1935 /* Handle special case where both bytes have the same length */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1936 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1937 if (from_len == to_len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1938 if (from_len == 1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1939 return replace_single_character_in_place( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1940 self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1941 from_s[0], |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1942 to_s[0], |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1943 maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1944 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1945 return replace_substring_in_place( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1946 self, from_s, from_len, to_s, to_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1947 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1948 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1949 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1950 /* Otherwise use the more generic algorithms */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1951 if (from_len == 1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1952 return replace_single_character(self, from_s[0], |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1953 to_s, to_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1954 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1955 /* len('from')>=2, len('to')>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1956 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1957 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1958 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1959 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1960 PyDoc_STRVAR(replace__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1961 "B.replace (old, new[, count]) -> bytes\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1962 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1963 Return a copy of bytes B with all occurrences of subsection\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1964 old replaced by new. If the optional argument count is\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1965 given, only the first count occurrences are replaced."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1966 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1967 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1968 bytes_replace(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1969 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1970 Py_ssize_t count = -1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1971 PyObject *from, *to; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1972 const char *from_s, *to_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1973 Py_ssize_t from_len, to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1974 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1975 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1976 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1977 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1978 if (PyBytes_Check(from)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1979 from_s = PyBytes_AS_STRING(from); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1980 from_len = PyBytes_GET_SIZE(from); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1981 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1982 else if (PyObject_AsCharBuffer(from, &from_s, &from_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1983 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1984 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1985 if (PyBytes_Check(to)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1986 to_s = PyBytes_AS_STRING(to); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1987 to_len = PyBytes_GET_SIZE(to); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1988 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1989 else if (PyObject_AsCharBuffer(to, &to_s, &to_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1990 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1991 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1992 return (PyObject *)replace((PyBytesObject *) self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1993 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1994 to_s, to_len, count); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1995 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1996 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1997 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1998 /* Overallocate the initial list to reduce the number of reallocs for small |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1999 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2000 resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2001 text (roughly 11 words per line) and field delimited data (usually 1-10 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2002 fields). For large strings the split algorithms are bandwidth limited |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2003 so increasing the preallocation likely will not improve things.*/ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2004 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2005 #define MAX_PREALLOC 12 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2006 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2007 /* 5 splits gives 6 elements */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2008 #define PREALLOC_SIZE(maxsplit) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2009 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2010 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2011 #define SPLIT_APPEND(data, left, right) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2012 str = PyBytes_FromStringAndSize((data) + (left), \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2013 (right) - (left)); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2014 if (str == NULL) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2015 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2016 if (PyList_Append(list, str)) { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2017 Py_DECREF(str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2018 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2019 } \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2020 else \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2021 Py_DECREF(str); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2022 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2023 #define SPLIT_ADD(data, left, right) { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2024 str = PyBytes_FromStringAndSize((data) + (left), \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2025 (right) - (left)); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2026 if (str == NULL) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2027 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2028 if (count < MAX_PREALLOC) { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2029 PyList_SET_ITEM(list, count, str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2030 } else { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2031 if (PyList_Append(list, str)) { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2032 Py_DECREF(str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2033 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2034 } \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2035 else \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2036 Py_DECREF(str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2037 } \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2038 count++; } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2039 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2040 /* Always force the list to the expected size. */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2041 #define FIX_PREALLOC_SIZE(list) ((PyListObject *)list)->ob_size = count |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2042 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2043 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2044 Py_LOCAL_INLINE(PyObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2045 split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2046 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2047 register Py_ssize_t i, j, count=0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2048 PyObject *str; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2049 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2050 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2051 if (list == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2052 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2053 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2054 i = j = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2055 while ((j < len) && (maxcount-- > 0)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2056 for(; j<len; j++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2057 /* I found that using memchr makes no difference */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2058 if (s[j] == ch) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2059 SPLIT_ADD(s, i, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2060 i = j = j + 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2061 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2062 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2063 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2064 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2065 if (i <= len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2066 SPLIT_ADD(s, i, len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2067 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2068 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2069 return list; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2070 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2071 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2072 Py_DECREF(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2073 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2074 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2075 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2076 PyDoc_STRVAR(split__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2077 "B.split(sep [,maxsplit]) -> list of bytes\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2078 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2079 Return a list of the bytes in the string B, using sep as the\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2080 delimiter. If maxsplit is given, at most maxsplit\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2081 splits are done."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2082 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2083 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2084 bytes_split(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2085 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2086 Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2087 Py_ssize_t maxsplit = -1, count=0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2088 const char *s = PyBytes_AS_STRING(self), *sub; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2089 PyObject *list, *str, *subobj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2090 #ifdef USE_FAST |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2091 Py_ssize_t pos; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2092 #endif |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2093 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2094 if (!PyArg_ParseTuple(args, "O|n:split", &subobj, &maxsplit)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2095 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2096 if (maxsplit < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2097 maxsplit = PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2098 if (PyBytes_Check(subobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2099 sub = PyBytes_AS_STRING(subobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2100 n = PyBytes_GET_SIZE(subobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2101 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2102 else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2103 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2104 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2105 if (n == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2106 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2107 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2108 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2109 else if (n == 1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2110 return split_char(s, len, sub[0], maxsplit); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2111 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2112 list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2113 if (list == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2114 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2115 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2116 #ifdef USE_FAST |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2117 i = j = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2118 while (maxsplit-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2119 pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2120 if (pos < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2121 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2122 j = i+pos; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2123 SPLIT_ADD(s, i, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2124 i = j + n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2125 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2126 #else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2127 i = j = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2128 while ((j+n <= len) && (maxsplit-- > 0)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2129 for (; j+n <= len; j++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2130 if (Py_STRING_MATCH(s, j, sub, n)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2131 SPLIT_ADD(s, i, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2132 i = j = j + n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2133 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2134 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2135 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2136 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2137 #endif |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2138 SPLIT_ADD(s, i, len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2139 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2140 return list; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2141 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2142 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2143 Py_DECREF(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2144 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2145 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2146 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2147 PyDoc_STRVAR(partition__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2148 "B.partition(sep) -> (head, sep, tail)\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2149 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2150 Searches for the separator sep in B, and returns the part before it,\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2151 the separator itself, and the part after it. If the separator is not\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2152 found, returns B and two empty bytes."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2153 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2154 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2155 bytes_partition(PyBytesObject *self, PyObject *sep_obj) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2156 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2157 PyObject *bytesep, *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2158 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2159 bytesep = PyBytes_FromObject(sep_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2160 if (! bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2161 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2162 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2163 result = stringlib_partition( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2164 (PyObject*) self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2165 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
2166 bytesep, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2167 PyBytes_AS_STRING(bytesep), PyBytes_GET_SIZE(bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2168 ); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2169 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2170 Py_DECREF(bytesep); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2171 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2172 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2173 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2174 PyDoc_STRVAR(rpartition__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2175 "B.rpartition(sep) -> (tail, sep, head)\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2176 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2177 Searches for the separator sep in B, starting at the end of B, and returns\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2178 the part before it, the separator itself, and the part after it. If the\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2179 separator is not found, returns two empty bytes and B."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2180 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2181 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2182 bytes_rpartition(PyBytesObject *self, PyObject *sep_obj) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2183 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2184 PyObject *bytesep, *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2185 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2186 bytesep = PyBytes_FromObject(sep_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2187 if (! bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2188 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2189 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2190 result = stringlib_rpartition( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2191 (PyObject*) self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2192 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
2193 bytesep, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2194 PyBytes_AS_STRING(bytesep), PyBytes_GET_SIZE(bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2195 ); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2196 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2197 Py_DECREF(bytesep); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2198 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2199 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2200 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2201 Py_LOCAL_INLINE(PyObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2202 rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2203 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2204 register Py_ssize_t i, j, count=0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2205 PyObject *str; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2206 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2207 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2208 if (list == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2209 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2210 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2211 i = j = len - 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2212 while ((i >= 0) && (maxcount-- > 0)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2213 for (; i >= 0; i--) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2214 if (s[i] == ch) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2215 SPLIT_ADD(s, i + 1, j + 1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2216 j = i = i - 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2217 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2218 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2219 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2220 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2221 if (j >= -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2222 SPLIT_ADD(s, 0, j + 1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2223 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2224 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2225 if (PyList_Reverse(list) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2226 goto onError; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2227 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2228 return list; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2229 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2230 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2231 Py_DECREF(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2232 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2233 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2234 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2235 PyDoc_STRVAR(rsplit__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2236 "B.rsplit(sep [,maxsplit]) -> list of bytes\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2237 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2238 Return a list of the sections in the byte B, using sep as the\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2239 delimiter, starting at the end of the bytes and working\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2240 to the front. If maxsplit is given, at most maxsplit splits are\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2241 done."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2242 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2243 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2244 bytes_rsplit(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2245 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2246 Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2247 Py_ssize_t maxsplit = -1, count=0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2248 const char *s = PyBytes_AS_STRING(self), *sub; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2249 PyObject *list, *str, *subobj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2250 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2251 if (!PyArg_ParseTuple(args, "O|n:rsplit", &subobj, &maxsplit)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2252 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2253 if (maxsplit < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2254 maxsplit = PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2255 if (PyBytes_Check(subobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2256 sub = PyBytes_AS_STRING(subobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2257 n = PyBytes_GET_SIZE(subobj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2258 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2259 else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2260 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2261 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2262 if (n == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2263 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2264 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2265 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2266 else if (n == 1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2267 return rsplit_char(s, len, sub[0], maxsplit); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2268 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2269 list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2270 if (list == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2271 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2272 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2273 j = len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2274 i = j - n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2275 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2276 while ( (i >= 0) && (maxsplit-- > 0) ) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2277 for (; i>=0; i--) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2278 if (Py_STRING_MATCH(s, i, sub, n)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2279 SPLIT_ADD(s, i + n, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2280 j = i; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2281 i -= n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2282 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2283 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2284 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2285 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2286 SPLIT_ADD(s, 0, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2287 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2288 if (PyList_Reverse(list) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2289 goto onError; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2290 return list; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2291 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2292 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2293 Py_DECREF(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2294 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2295 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2296 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2297 PyDoc_STRVAR(extend__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2298 "B.extend(iterable int) -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2299 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2300 Append all the elements from the iterator or sequence to the\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2301 end of the bytes."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2302 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2303 bytes_extend(PyBytesObject *self, PyObject *arg) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2304 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2305 if (bytes_setslice(self, self->ob_size, self->ob_size, arg) == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2306 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2307 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2308 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2309 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2310 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2311 PyDoc_STRVAR(reverse__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2312 "B.reverse() -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2313 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2314 Reverse the order of the values in bytes in place."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2315 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2316 bytes_reverse(PyBytesObject *self, PyObject *unused) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2317 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2318 char swap, *head, *tail; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2319 Py_ssize_t i, j, n = self->ob_size; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2320 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2321 j = n / 2; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2322 head = self->ob_bytes; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2323 tail = head + n - 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2324 for (i = 0; i < j; i++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2325 swap = *head; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2326 *head++ = *tail; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2327 *tail-- = swap; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2328 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2329 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2330 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2331 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2332 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2333 PyDoc_STRVAR(insert__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2334 "B.insert(index, int) -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2335 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2336 Insert a single item into the bytes before the given index."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2337 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2338 bytes_insert(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2339 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2340 int value; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2341 Py_ssize_t where, n = self->ob_size; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2342 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2343 if (!PyArg_ParseTuple(args, "ni:insert", &where, &value)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2344 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2345 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2346 if (n == PY_SSIZE_T_MAX) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2347 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2348 "cannot add more objects to bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2349 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2350 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2351 if (value < 0 || value >= 256) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2352 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2353 "byte must be in range(0, 256)"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2354 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2355 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2356 if (PyBytes_Resize((PyObject *)self, n + 1) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2357 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2358 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2359 if (where < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2360 where += n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2361 if (where < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2362 where = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2363 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2364 if (where > n) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2365 where = n; |
|
40982
431b3238593e
Fix off-by-one bug in memmove() call in bytes_insert().
Guido van Rossum <guido@python.org>
parents:
40978
diff
changeset
|
2366 memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2367 self->ob_bytes[where] = value; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2368 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2369 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2370 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2371 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2372 PyDoc_STRVAR(append__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2373 "B.append(int) -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2374 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2375 Append a single item to the end of the bytes."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2376 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2377 bytes_append(PyBytesObject *self, PyObject *arg) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2378 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2379 int value; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2380 Py_ssize_t n = self->ob_size; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2381 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2382 if (! _getbytevalue(arg, &value)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2383 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2384 if (n == PY_SSIZE_T_MAX) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2385 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2386 "cannot add more objects to bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2387 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2388 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2389 if (PyBytes_Resize((PyObject *)self, n + 1) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2390 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2391 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2392 self->ob_bytes[n] = value; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2393 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2394 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2395 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2396 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2397 PyDoc_STRVAR(pop__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2398 "B.pop([index]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2399 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2400 Remove and return a single item from the bytes. If no index\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2401 argument is give, will pop the last value."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2402 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2403 bytes_pop(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2404 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2405 int value; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2406 Py_ssize_t where = -1, n = self->ob_size; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2407 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2408 if (!PyArg_ParseTuple(args, "|n:pop", &where)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2409 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2410 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2411 if (n == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2412 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2413 "cannot pop an empty bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2414 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2415 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2416 if (where < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2417 where += self->ob_size; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2418 if (where < 0 || where >= self->ob_size) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2419 PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2420 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2421 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2422 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2423 value = self->ob_bytes[where]; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2424 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2425 if (PyBytes_Resize((PyObject *)self, n - 1) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2426 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2427 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2428 return PyInt_FromLong(value); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2429 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2430 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2431 PyDoc_STRVAR(remove__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2432 "B.remove(int) -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2433 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2434 Remove the first occurance of a value in bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2435 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2436 bytes_remove(PyBytesObject *self, PyObject *arg) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2437 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2438 int value; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2439 Py_ssize_t where, n = self->ob_size; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2440 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2441 if (! _getbytevalue(arg, &value)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2442 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2443 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2444 for (where = 0; where < n; where++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2445 if (self->ob_bytes[where] == value) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2446 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2447 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2448 if (where == n) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2449 PyErr_SetString(PyExc_ValueError, "value not found in bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2450 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2451 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2452 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2453 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2454 if (PyBytes_Resize((PyObject *)self, n - 1) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2455 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2456 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2457 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2458 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2459 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2460 /* XXX These two helpers could be optimized if argsize == 1 */ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2461 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2462 Py_ssize_t |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2463 lstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2464 void *argptr, Py_ssize_t argsize) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2465 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2466 Py_ssize_t i = 0; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2467 while (i < mysize && memchr(argptr, myptr[i], argsize)) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2468 i++; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2469 return i; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2470 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2471 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2472 Py_ssize_t |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2473 rstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2474 void *argptr, Py_ssize_t argsize) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2475 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2476 Py_ssize_t i = mysize - 1; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2477 while (i >= 0 && memchr(argptr, myptr[i], argsize)) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2478 i--; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2479 return i + 1; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2480 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2481 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2482 PyDoc_STRVAR(strip__doc__, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2483 "B.strip(bytes) -> bytes\n\ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2484 \n\ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2485 Strip leading and trailing bytes contained in the argument."); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2486 static PyObject * |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2487 bytes_strip(PyBytesObject *self, PyObject *arg) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2488 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2489 Py_ssize_t left, right, mysize, argsize; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2490 void *myptr, *argptr; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2491 if (arg == NULL || !PyBytes_Check(arg)) { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2492 PyErr_SetString(PyExc_TypeError, "strip() requires a bytes argument"); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2493 return NULL; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2494 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2495 myptr = self->ob_bytes; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2496 mysize = self->ob_size; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2497 argptr = ((PyBytesObject *)arg)->ob_bytes; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2498 argsize = ((PyBytesObject *)arg)->ob_size; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2499 left = lstrip_helper(myptr, mysize, argptr, argsize); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2500 right = rstrip_helper(myptr, mysize, argptr, argsize); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2501 return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2502 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2503 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2504 PyDoc_STRVAR(lstrip__doc__, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2505 "B.lstrip(bytes) -> bytes\n\ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2506 \n\ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2507 Strip leading bytes contained in the argument."); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2508 static PyObject * |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2509 bytes_lstrip(PyBytesObject *self, PyObject *arg) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2510 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2511 Py_ssize_t left, right, mysize, argsize; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2512 void *myptr, *argptr; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2513 if (arg == NULL || !PyBytes_Check(arg)) { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2514 PyErr_SetString(PyExc_TypeError, "strip() requires a bytes argument"); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2515 return NULL; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2516 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2517 myptr = self->ob_bytes; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2518 mysize = self->ob_size; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2519 argptr = ((PyBytesObject *)arg)->ob_bytes; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2520 argsize = ((PyBytesObject *)arg)->ob_size; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2521 left = lstrip_helper(myptr, mysize, argptr, argsize); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2522 right = mysize; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2523 return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2524 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2525 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2526 PyDoc_STRVAR(rstrip__doc__, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2527 "B.rstrip(bytes) -> bytes\n\ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2528 \n\ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2529 Strip trailing bytes contained in the argument."); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2530 static PyObject * |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2531 bytes_rstrip(PyBytesObject *self, PyObject *arg) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2532 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2533 Py_ssize_t left, right, mysize, argsize; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2534 void *myptr, *argptr; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2535 if (arg == NULL || !PyBytes_Check(arg)) { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2536 PyErr_SetString(PyExc_TypeError, "strip() requires a bytes argument"); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2537 return NULL; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2538 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2539 myptr = self->ob_bytes; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2540 mysize = self->ob_size; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2541 argptr = ((PyBytesObject *)arg)->ob_bytes; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2542 argsize = ((PyBytesObject *)arg)->ob_size; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2543 left = 0; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2544 right = rstrip_helper(myptr, mysize, argptr, argsize); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2545 return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2546 } |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2547 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2548 PyDoc_STRVAR(decode_doc, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2549 "B.decode([encoding[,errors]]) -> unicode obect.\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2550 \n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2551 Decodes B using the codec registered for encoding. encoding defaults\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2552 to the default encoding. errors may be given to set a different error\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2553 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2554 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2555 as well as any other name registerd with codecs.register_error that is\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2556 able to handle UnicodeDecodeErrors."); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2557 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2558 static PyObject * |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2559 bytes_decode(PyObject *self, PyObject *args) |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
2560 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2561 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2562 const char *errors = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2563 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2564 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2565 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2566 if (encoding == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2567 encoding = PyUnicode_GetDefaultEncoding(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2568 return PyCodec_Decode(self, encoding, errors); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2569 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2570 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2571 PyDoc_STRVAR(alloc_doc, |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2572 "B.__alloc__() -> int\n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2573 \n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2574 Returns the number of bytes actually allocated."); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2575 |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2576 static PyObject * |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2577 bytes_alloc(PyBytesObject *self) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2578 { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2579 return PyInt_FromSsize_t(self->ob_alloc); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2580 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2581 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2582 PyDoc_STRVAR(join_doc, |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2583 "bytes.join(iterable_of_bytes) -> bytes\n\ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2584 \n\ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2585 Concatenates any number of bytes objects. Example:\n\ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2586 bytes.join([bytes('ab'), bytes('pq'), bytes('rs')]) -> bytes('abpqrs')."); |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2587 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2588 static PyObject * |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2589 bytes_join(PyObject *cls, PyObject *it) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2590 { |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2591 PyObject *seq; |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2592 Py_ssize_t i; |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2593 Py_ssize_t n; |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2594 PyObject **items; |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2595 Py_ssize_t totalsize = 0; |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2596 PyObject *result; |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2597 char *dest; |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2598 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2599 seq = PySequence_Fast(it, "can only join an iterable"); |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2600 if (seq == NULL) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2601 return NULL; |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2602 n = PySequence_Fast_GET_SIZE(seq); |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2603 items = PySequence_Fast_ITEMS(seq); |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2604 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2605 /* Compute the total size, and check that they are all bytes */ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2606 for (i = 0; i < n; i++) { |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2607 PyObject *obj = items[i]; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2608 if (!PyBytes_Check(obj)) { |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2609 PyErr_Format(PyExc_TypeError, |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2610 "can only join an iterable of bytes " |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2611 "(item %ld has type '%.100s')", |
|
39222
b0cdcbcc5f45
Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents:
38117
diff
changeset
|
2612 /* XXX %ld isn't right on Win64 */ |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2613 (long)i, obj->ob_type->tp_name); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2614 goto error; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2615 } |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2616 totalsize += PyBytes_GET_SIZE(obj); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2617 if (totalsize < 0) { |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2618 PyErr_NoMemory(); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2619 goto error; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2620 } |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2621 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2622 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2623 /* Allocate the result, and copy the bytes */ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2624 result = PyBytes_FromStringAndSize(NULL, totalsize); |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2625 if (result == NULL) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2626 goto error; |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2627 dest = PyBytes_AS_STRING(result); |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2628 for (i = 0; i < n; i++) { |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2629 PyObject *obj = items[i]; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2630 Py_ssize_t size = PyBytes_GET_SIZE(obj); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2631 memcpy(dest, PyBytes_AS_STRING(obj), size); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2632 dest += size; |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2633 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2634 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2635 /* Done */ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2636 Py_DECREF(seq); |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2637 return result; |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2638 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2639 /* Error handling */ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2640 error: |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2641 Py_DECREF(seq); |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2642 return NULL; |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2643 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2644 |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2645 PyDoc_STRVAR(fromhex_doc, |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2646 "bytes.fromhex(string) -> bytes\n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2647 \n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2648 Create a bytes object from a string of hexadecimal numbers.\n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2649 Spaces between two numbers are accepted. Example:\n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2650 bytes.fromhex('10 2030') -> bytes([0x10, 0x20, 0x30])."); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2651 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2652 static int |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2653 hex_digit_to_int(int c) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2654 { |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2655 if (isdigit(c)) |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2656 return c - '0'; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2657 else { |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2658 if (isupper(c)) |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2659 c = tolower(c); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2660 if (c >= 'a' && c <= 'f') |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2661 return c - 'a' + 10; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2662 } |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2663 return -1; |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2664 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2665 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2666 static PyObject * |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2667 bytes_fromhex(PyObject *cls, PyObject *args) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2668 { |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2669 PyObject *newbytes; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2670 char *hex, *buf; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2671 Py_ssize_t len, byteslen, i, j; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2672 int top, bot; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2673 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2674 if (!PyArg_ParseTuple(args, "s#:fromhex", &hex, &len)) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2675 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2676 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2677 byteslen = len / 2; /* max length if there are no spaces */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2678 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2679 newbytes = PyBytes_FromStringAndSize(NULL, byteslen); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2680 if (!newbytes) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2681 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2682 buf = PyBytes_AS_STRING(newbytes); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2683 |
|
41598
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
2684 for (i = j = 0; i < len; i += 2) { |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2685 /* skip over spaces in the input */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2686 while (Py_CHARMASK(hex[i]) == ' ') |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2687 i++; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2688 if (i >= len) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2689 break; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2690 top = hex_digit_to_int(Py_CHARMASK(hex[i])); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2691 bot = hex_digit_to_int(Py_CHARMASK(hex[i+1])); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2692 if (top == -1 || bot == -1) { |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2693 PyErr_Format(PyExc_ValueError, |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2694 "non-hexadecimal number string '%c%c' found in " |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2695 "fromhex() arg at position %zd", |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2696 hex[i], hex[i+1], i); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2697 goto error; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2698 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2699 buf[j++] = (top << 4) + bot; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2700 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2701 if (PyBytes_Resize(newbytes, j) < 0) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2702 goto error; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2703 return newbytes; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2704 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2705 error: |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2706 Py_DECREF(newbytes); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2707 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2708 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2709 |
|
41426
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2710 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2711 |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2712 static PyObject * |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2713 bytes_reduce(PyBytesObject *self) |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2714 { |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2715 return Py_BuildValue("(O(s#))", |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2716 self->ob_type, |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2717 self->ob_bytes == NULL ? "" : self->ob_bytes, |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2718 self->ob_size); |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2719 } |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2720 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2721 static PySequenceMethods bytes_as_sequence = { |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2722 (lenfunc)bytes_length, /* sq_length */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2723 (binaryfunc)bytes_concat, /* sq_concat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2724 (ssizeargfunc)bytes_repeat, /* sq_repeat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2725 (ssizeargfunc)bytes_getitem, /* sq_item */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2726 0, /* sq_slice */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2727 (ssizeobjargproc)bytes_setitem, /* sq_ass_item */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2728 0, /* sq_ass_slice */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2729 (objobjproc)bytes_contains, /* sq_contains */ |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2730 (binaryfunc)bytes_iconcat, /* sq_inplace_concat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2731 (ssizeargfunc)bytes_irepeat, /* sq_inplace_repeat */ |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2732 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2733 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2734 static PyMappingMethods bytes_as_mapping = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2735 (lenfunc)bytes_length, |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
2736 (binaryfunc)bytes_subscript, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
2737 (objobjargproc)bytes_ass_subscript, |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2738 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2739 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2740 static PyBufferProcs bytes_as_buffer = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2741 (readbufferproc)bytes_getbuffer, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2742 (writebufferproc)bytes_getbuffer, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2743 (segcountproc)bytes_getsegcount, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2744 /* XXX Bytes are not characters! But we need to implement |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2745 bf_getcharbuffer() so we can be used as 't#' argument to codecs. */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2746 (charbufferproc)bytes_getbuffer, |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2747 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2748 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2749 static PyMethodDef |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2750 bytes_methods[] = { |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2751 {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2752 {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2753 {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2754 {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2755 {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2756 {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, endswith__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2757 {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2758 startswith__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2759 {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2760 {"translate", (PyCFunction)bytes_translate, METH_VARARGS, translate__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2761 {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2762 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, rpartition__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2763 {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2764 {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2765 {"extend", (PyCFunction)bytes_extend, METH_O, extend__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2766 {"insert", (PyCFunction)bytes_insert, METH_VARARGS, insert__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2767 {"append", (PyCFunction)bytes_append, METH_O, append__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2768 {"reverse", (PyCFunction)bytes_reverse, METH_NOARGS, reverse__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2769 {"pop", (PyCFunction)bytes_pop, METH_VARARGS, pop__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2770 {"remove", (PyCFunction)bytes_remove, METH_O, remove__doc__}, |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2771 {"strip", (PyCFunction)bytes_strip, METH_O, strip__doc__}, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2772 {"lstrip", (PyCFunction)bytes_lstrip, METH_O, lstrip__doc__}, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2773 {"rstrip", (PyCFunction)bytes_rstrip, METH_O, rstrip__doc__}, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2774 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc}, |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2775 {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc}, |
|
41426
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2776 {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS, |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2777 fromhex_doc}, |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2778 {"join", (PyCFunction)bytes_join, METH_O|METH_CLASS, join_doc}, |
|
41426
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2779 {"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc}, |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2780 {NULL} |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2781 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2782 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2783 PyDoc_STRVAR(bytes_doc, |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2784 "bytes([iterable]) -> new array of bytes.\n\ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2785 \n\ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2786 If an argument is given it must be an iterable yielding ints in range(256)."); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2787 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2788 PyTypeObject PyBytes_Type = { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2789 PyObject_HEAD_INIT(&PyType_Type) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2790 0, |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2791 "bytes", |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2792 sizeof(PyBytesObject), |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2793 0, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2794 (destructor)bytes_dealloc, /* tp_dealloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2795 0, /* tp_print */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2796 0, /* tp_getattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2797 0, /* tp_setattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2798 0, /* tp_compare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2799 (reprfunc)bytes_repr, /* tp_repr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2800 0, /* tp_as_number */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2801 &bytes_as_sequence, /* tp_as_sequence */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2802 &bytes_as_mapping, /* tp_as_mapping */ |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2803 0, /* tp_hash */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2804 0, /* tp_call */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2805 (reprfunc)bytes_str, /* tp_str */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2806 PyObject_GenericGetAttr, /* tp_getattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2807 0, /* tp_setattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2808 &bytes_as_buffer, /* tp_as_buffer */ |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2809 /* bytes is 'final' or 'sealed' */ |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2810 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2811 bytes_doc, /* tp_doc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2812 0, /* tp_traverse */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2813 0, /* tp_clear */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2814 (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2815 0, /* tp_weaklistoffset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2816 0, /* tp_iter */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2817 0, /* tp_iternext */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2818 bytes_methods, /* tp_methods */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2819 0, /* tp_members */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2820 0, /* tp_getset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2821 0, /* tp_base */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2822 0, /* tp_dict */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2823 0, /* tp_descr_get */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2824 0, /* tp_descr_set */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2825 0, /* tp_dictoffset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2826 (initproc)bytes_init, /* tp_init */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2827 PyType_GenericAlloc, /* tp_alloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2828 PyType_GenericNew, /* tp_new */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2829 PyObject_Del, /* tp_free */ |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2830 }; |
