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