Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1 | /* Bytes object implementation */ |
| 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 | |
| 37 | if (PyInt_Check(arg)) { |
| 38 | face_value = PyInt_AsLong(arg); |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 350 | PyErr_SetString(PyExc_IndexError, "buffer 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 | } |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 353 | return PyInt_FromLong((unsigned char)(self->ob_bytes[i])); |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 369 | PyErr_SetString(PyExc_IndexError, "buffer index out of range"); |
Thomas Wouters | 376446d | 2006-12-19 08:30:14 +0000 | [diff] [blame] | 370 | return NULL; |
| 371 | } |
| 372 | return PyInt_FromLong((unsigned char)(self->ob_bytes[i])); |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 406 | PyErr_SetString(PyExc_TypeError, "buffer 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 506 | PyErr_SetString(PyExc_IndexError, "buffer 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 542 | PyErr_SetString(PyExc_IndexError, "buffer 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 574 | PyErr_SetString(PyExc_TypeError, "buffer 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 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 760 | /* Use the modern buffer interface */ |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 838 | const char *quote_prefix = "buffer(b"; |
| 839 | const char *quote_postfix = ")"; |
| 840 | Py_ssize_t length = Py_Size(self); |
| 841 | /* 9 prefix + 2 postfix */ |
| 842 | size_t newsize = 11 + 4 * length; |
Georg Brandl | ee91be4 | 2007-02-24 19:41:35 +0000 | [diff] [blame] | 843 | PyObject *v; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 844 | if (newsize > PY_SSIZE_T_MAX || newsize / 4 - 2 != length) { |
Georg Brandl | ee91be4 | 2007-02-24 19:41:35 +0000 | [diff] [blame] | 845 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 846 | "buffer 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, |
| 924 | "str() on a buffer instance", 1)) |
| 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, |
| 946 | "Comparsion between buffer and string", 1)) |
| 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; |
| 1094 | return PyInt_FromSsize_t(result); |
| 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 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 1122 | count_obj = PyInt_FromSsize_t( |
| 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 | } |
| 1146 | return PyInt_FromSsize_t(result); |
| 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; |
| 1165 | return PyInt_FromSsize_t(result); |
| 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 | } |
| 1185 | return PyInt_FromSsize_t(result); |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 1338 | "B.translate(table[, deletechars]) -> buffer\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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2186 | "B.split([sep[, maxsplit]]) -> list of buffer\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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2295 | found, returns B and two empty buffer."); |
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\ |
| 2323 | buffer 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2420 | "B.rsplit(sep[, maxsplit]) -> list of buffer\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 | |
| 2490 | PyDoc_STRVAR(extend__doc__, |
| 2491 | "B.extend(iterable int) -> None\n\ |
| 2492 | \n\ |
| 2493 | Append all the elements from the iterator or sequence to the\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2494 | end of B."); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2495 | static PyObject * |
| 2496 | bytes_extend(PyBytesObject *self, PyObject *arg) |
| 2497 | { |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 2498 | /* XXX(gps): The docstring says any iterable int will do but the |
| 2499 | * bytes_setslice code only accepts something supporting PEP 3118. |
| 2500 | * A list or tuple of 0 <= int <= 255 is supposed to work. */ |
| 2501 | /* bug being tracked on: http://bugs.python.org/issue1283 */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2502 | if (bytes_setslice(self, Py_Size(self), Py_Size(self), arg) == -1) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2503 | return NULL; |
| 2504 | Py_RETURN_NONE; |
| 2505 | } |
| 2506 | |
| 2507 | |
| 2508 | PyDoc_STRVAR(reverse__doc__, |
| 2509 | "B.reverse() -> None\n\ |
| 2510 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2511 | Reverse the order of the values in B in place."); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2512 | static PyObject * |
| 2513 | bytes_reverse(PyBytesObject *self, PyObject *unused) |
| 2514 | { |
| 2515 | char swap, *head, *tail; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2516 | Py_ssize_t i, j, n = Py_Size(self); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2517 | |
| 2518 | j = n / 2; |
| 2519 | head = self->ob_bytes; |
| 2520 | tail = head + n - 1; |
| 2521 | for (i = 0; i < j; i++) { |
| 2522 | swap = *head; |
| 2523 | *head++ = *tail; |
| 2524 | *tail-- = swap; |
| 2525 | } |
| 2526 | |
| 2527 | Py_RETURN_NONE; |
| 2528 | } |
| 2529 | |
| 2530 | PyDoc_STRVAR(insert__doc__, |
| 2531 | "B.insert(index, int) -> None\n\ |
| 2532 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2533 | Insert a single item into the buffer before the given index."); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2534 | static PyObject * |
| 2535 | bytes_insert(PyBytesObject *self, PyObject *args) |
| 2536 | { |
| 2537 | int value; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2538 | Py_ssize_t where, n = Py_Size(self); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2539 | |
| 2540 | if (!PyArg_ParseTuple(args, "ni:insert", &where, &value)) |
| 2541 | return NULL; |
| 2542 | |
| 2543 | if (n == PY_SSIZE_T_MAX) { |
| 2544 | PyErr_SetString(PyExc_OverflowError, |
| 2545 | "cannot add more objects to bytes"); |
| 2546 | return NULL; |
| 2547 | } |
| 2548 | if (value < 0 || value >= 256) { |
| 2549 | PyErr_SetString(PyExc_ValueError, |
| 2550 | "byte must be in range(0, 256)"); |
| 2551 | return NULL; |
| 2552 | } |
| 2553 | if (PyBytes_Resize((PyObject *)self, n + 1) < 0) |
| 2554 | return NULL; |
| 2555 | |
| 2556 | if (where < 0) { |
| 2557 | where += n; |
| 2558 | if (where < 0) |
| 2559 | where = 0; |
| 2560 | } |
| 2561 | if (where > n) |
| 2562 | where = n; |
Guido van Rossum | 4fc8ae4 | 2007-02-27 20:57:45 +0000 | [diff] [blame] | 2563 | memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2564 | self->ob_bytes[where] = value; |
| 2565 | |
| 2566 | Py_RETURN_NONE; |
| 2567 | } |
| 2568 | |
| 2569 | PyDoc_STRVAR(append__doc__, |
| 2570 | "B.append(int) -> None\n\ |
| 2571 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2572 | Append a single item to the end of B."); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2573 | static PyObject * |
| 2574 | bytes_append(PyBytesObject *self, PyObject *arg) |
| 2575 | { |
| 2576 | int value; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2577 | Py_ssize_t n = Py_Size(self); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2578 | |
| 2579 | if (! _getbytevalue(arg, &value)) |
| 2580 | return NULL; |
| 2581 | if (n == PY_SSIZE_T_MAX) { |
| 2582 | PyErr_SetString(PyExc_OverflowError, |
| 2583 | "cannot add more objects to bytes"); |
| 2584 | return NULL; |
| 2585 | } |
| 2586 | if (PyBytes_Resize((PyObject *)self, n + 1) < 0) |
| 2587 | return NULL; |
| 2588 | |
| 2589 | self->ob_bytes[n] = value; |
| 2590 | |
| 2591 | Py_RETURN_NONE; |
| 2592 | } |
| 2593 | |
| 2594 | PyDoc_STRVAR(pop__doc__, |
| 2595 | "B.pop([index]) -> int\n\ |
| 2596 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2597 | Remove and return a single item from B. If no index\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2598 | argument is give, will pop the last value."); |
| 2599 | static PyObject * |
| 2600 | bytes_pop(PyBytesObject *self, PyObject *args) |
| 2601 | { |
| 2602 | int value; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2603 | Py_ssize_t where = -1, n = Py_Size(self); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2604 | |
| 2605 | if (!PyArg_ParseTuple(args, "|n:pop", &where)) |
| 2606 | return NULL; |
| 2607 | |
| 2608 | if (n == 0) { |
| 2609 | PyErr_SetString(PyExc_OverflowError, |
| 2610 | "cannot pop an empty bytes"); |
| 2611 | return NULL; |
| 2612 | } |
| 2613 | if (where < 0) |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2614 | where += Py_Size(self); |
| 2615 | if (where < 0 || where >= Py_Size(self)) { |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2616 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 2617 | return NULL; |
| 2618 | } |
| 2619 | |
| 2620 | value = self->ob_bytes[where]; |
| 2621 | memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
| 2622 | if (PyBytes_Resize((PyObject *)self, n - 1) < 0) |
| 2623 | return NULL; |
| 2624 | |
| 2625 | return PyInt_FromLong(value); |
| 2626 | } |
| 2627 | |
| 2628 | PyDoc_STRVAR(remove__doc__, |
| 2629 | "B.remove(int) -> None\n\ |
| 2630 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2631 | Remove the first occurance of a value in B."); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2632 | static PyObject * |
| 2633 | bytes_remove(PyBytesObject *self, PyObject *arg) |
| 2634 | { |
| 2635 | int value; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2636 | Py_ssize_t where, n = Py_Size(self); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2637 | |
| 2638 | if (! _getbytevalue(arg, &value)) |
| 2639 | return NULL; |
| 2640 | |
| 2641 | for (where = 0; where < n; where++) { |
| 2642 | if (self->ob_bytes[where] == value) |
| 2643 | break; |
| 2644 | } |
| 2645 | if (where == n) { |
| 2646 | PyErr_SetString(PyExc_ValueError, "value not found in bytes"); |
| 2647 | return NULL; |
| 2648 | } |
| 2649 | |
| 2650 | memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
| 2651 | if (PyBytes_Resize((PyObject *)self, n - 1) < 0) |
| 2652 | return NULL; |
| 2653 | |
| 2654 | Py_RETURN_NONE; |
| 2655 | } |
| 2656 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2657 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 2658 | |
Neal Norwitz | 2bad970 | 2007-08-27 06:19:22 +0000 | [diff] [blame] | 2659 | static Py_ssize_t |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2660 | lstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
| 2661 | void *argptr, Py_ssize_t argsize) |
| 2662 | { |
| 2663 | Py_ssize_t i = 0; |
| 2664 | while (i < mysize && memchr(argptr, myptr[i], argsize)) |
| 2665 | i++; |
| 2666 | return i; |
| 2667 | } |
| 2668 | |
Neal Norwitz | 2bad970 | 2007-08-27 06:19:22 +0000 | [diff] [blame] | 2669 | static Py_ssize_t |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2670 | rstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
| 2671 | void *argptr, Py_ssize_t argsize) |
| 2672 | { |
| 2673 | Py_ssize_t i = mysize - 1; |
| 2674 | while (i >= 0 && memchr(argptr, myptr[i], argsize)) |
| 2675 | i--; |
| 2676 | return i + 1; |
| 2677 | } |
| 2678 | |
| 2679 | PyDoc_STRVAR(strip__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2680 | "B.strip([bytes]) -> buffer\n\ |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2681 | \n\ |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2682 | Strip leading and trailing bytes contained in the argument.\n\ |
| 2683 | If the argument is omitted, strip ASCII whitespace."); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2684 | static PyObject * |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2685 | bytes_strip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2686 | { |
| 2687 | Py_ssize_t left, right, mysize, argsize; |
| 2688 | void *myptr, *argptr; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2689 | PyObject *arg = Py_None; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 2690 | Py_buffer varg; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2691 | if (!PyArg_ParseTuple(args, "|O:strip", &arg)) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2692 | return NULL; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2693 | if (arg == Py_None) { |
| 2694 | argptr = "\t\n\r\f\v "; |
| 2695 | argsize = 6; |
| 2696 | } |
| 2697 | else { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2698 | if (_getbuffer(arg, &varg) < 0) |
| 2699 | return NULL; |
| 2700 | argptr = varg.buf; |
| 2701 | argsize = varg.len; |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2702 | } |
| 2703 | myptr = self->ob_bytes; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2704 | mysize = Py_Size(self); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2705 | left = lstrip_helper(myptr, mysize, argptr, argsize); |
Guido van Rossum | eb29e9a | 2007-08-08 21:55:33 +0000 | [diff] [blame] | 2706 | if (left == mysize) |
| 2707 | right = left; |
| 2708 | else |
| 2709 | right = rstrip_helper(myptr, mysize, argptr, argsize); |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2710 | if (arg != Py_None) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2711 | PyObject_ReleaseBuffer(arg, &varg); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2712 | return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left); |
| 2713 | } |
| 2714 | |
| 2715 | PyDoc_STRVAR(lstrip__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2716 | "B.lstrip([bytes]) -> buffer\n\ |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2717 | \n\ |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2718 | Strip leading bytes contained in the argument.\n\ |
| 2719 | If the argument is omitted, strip leading ASCII whitespace."); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2720 | static PyObject * |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2721 | bytes_lstrip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2722 | { |
| 2723 | Py_ssize_t left, right, mysize, argsize; |
| 2724 | void *myptr, *argptr; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2725 | PyObject *arg = Py_None; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 2726 | Py_buffer varg; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2727 | if (!PyArg_ParseTuple(args, "|O:lstrip", &arg)) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2728 | return NULL; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2729 | if (arg == Py_None) { |
| 2730 | argptr = "\t\n\r\f\v "; |
| 2731 | argsize = 6; |
| 2732 | } |
| 2733 | else { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2734 | if (_getbuffer(arg, &varg) < 0) |
| 2735 | return NULL; |
| 2736 | argptr = varg.buf; |
| 2737 | argsize = varg.len; |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2738 | } |
| 2739 | myptr = self->ob_bytes; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2740 | mysize = Py_Size(self); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2741 | left = lstrip_helper(myptr, mysize, argptr, argsize); |
| 2742 | right = mysize; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2743 | if (arg != Py_None) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2744 | PyObject_ReleaseBuffer(arg, &varg); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2745 | return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left); |
| 2746 | } |
| 2747 | |
| 2748 | PyDoc_STRVAR(rstrip__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2749 | "B.rstrip([bytes]) -> buffer\n\ |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2750 | \n\ |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2751 | Strip trailing bytes contained in the argument.\n\ |
| 2752 | If the argument is omitted, strip trailing ASCII whitespace."); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2753 | static PyObject * |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2754 | bytes_rstrip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2755 | { |
| 2756 | Py_ssize_t left, right, mysize, argsize; |
| 2757 | void *myptr, *argptr; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2758 | PyObject *arg = Py_None; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 2759 | Py_buffer varg; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2760 | if (!PyArg_ParseTuple(args, "|O:rstrip", &arg)) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2761 | return NULL; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2762 | if (arg == Py_None) { |
| 2763 | argptr = "\t\n\r\f\v "; |
| 2764 | argsize = 6; |
| 2765 | } |
| 2766 | else { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2767 | if (_getbuffer(arg, &varg) < 0) |
| 2768 | return NULL; |
| 2769 | argptr = varg.buf; |
| 2770 | argsize = varg.len; |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2771 | } |
| 2772 | myptr = self->ob_bytes; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2773 | mysize = Py_Size(self); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2774 | left = 0; |
| 2775 | right = rstrip_helper(myptr, mysize, argptr, argsize); |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 2776 | if (arg != Py_None) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2777 | PyObject_ReleaseBuffer(arg, &varg); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2778 | return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left); |
| 2779 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2780 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2781 | PyDoc_STRVAR(decode_doc, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2782 | "B.decode([encoding[, errors]]) -> unicode object.\n\ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2783 | \n\ |
| 2784 | Decodes B using the codec registered for encoding. encoding defaults\n\ |
| 2785 | 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^] | 2786 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 2787 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2788 | as well as any other name registerd with codecs.register_error that is\n\ |
| 2789 | able to handle UnicodeDecodeErrors."); |
| 2790 | |
| 2791 | static PyObject * |
| 2792 | bytes_decode(PyObject *self, PyObject *args) |
Guido van Rossum | b6f1fdc | 2007-04-12 22:49:52 +0000 | [diff] [blame] | 2793 | { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2794 | const char *encoding = NULL; |
| 2795 | const char *errors = NULL; |
| 2796 | |
| 2797 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 2798 | return NULL; |
| 2799 | if (encoding == NULL) |
| 2800 | encoding = PyUnicode_GetDefaultEncoding(); |
| 2801 | return PyCodec_Decode(self, encoding, errors); |
| 2802 | } |
| 2803 | |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame] | 2804 | PyDoc_STRVAR(alloc_doc, |
| 2805 | "B.__alloc__() -> int\n\ |
| 2806 | \n\ |
| 2807 | Returns the number of bytes actually allocated."); |
| 2808 | |
| 2809 | static PyObject * |
| 2810 | bytes_alloc(PyBytesObject *self) |
| 2811 | { |
| 2812 | return PyInt_FromSsize_t(self->ob_alloc); |
| 2813 | } |
| 2814 | |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2815 | PyDoc_STRVAR(join_doc, |
Guido van Rossum | cd6ae68 | 2007-05-09 19:52:16 +0000 | [diff] [blame] | 2816 | "B.join(iterable_of_bytes) -> bytes\n\ |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2817 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2818 | Concatenates any number of buffer objects, with B in between each pair."); |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2819 | |
| 2820 | static PyObject * |
Guido van Rossum | cd6ae68 | 2007-05-09 19:52:16 +0000 | [diff] [blame] | 2821 | bytes_join(PyBytesObject *self, PyObject *it) |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2822 | { |
| 2823 | PyObject *seq; |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 2824 | Py_ssize_t mysize = Py_Size(self); |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2825 | Py_ssize_t i; |
| 2826 | Py_ssize_t n; |
| 2827 | PyObject **items; |
| 2828 | Py_ssize_t totalsize = 0; |
| 2829 | PyObject *result; |
| 2830 | char *dest; |
| 2831 | |
| 2832 | seq = PySequence_Fast(it, "can only join an iterable"); |
| 2833 | if (seq == NULL) |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2834 | return NULL; |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2835 | n = PySequence_Fast_GET_SIZE(seq); |
| 2836 | items = PySequence_Fast_ITEMS(seq); |
| 2837 | |
| 2838 | /* Compute the total size, and check that they are all bytes */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2839 | /* XXX Shouldn't we use _getbuffer() on these items instead? */ |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2840 | for (i = 0; i < n; i++) { |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2841 | PyObject *obj = items[i]; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2842 | if (!PyBytes_Check(obj) && !PyString_Check(obj)) { |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2843 | PyErr_Format(PyExc_TypeError, |
| 2844 | "can only join an iterable of bytes " |
| 2845 | "(item %ld has type '%.100s')", |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 2846 | /* XXX %ld isn't right on Win64 */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2847 | (long)i, Py_Type(obj)->tp_name); |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2848 | goto error; |
| 2849 | } |
Guido van Rossum | cd6ae68 | 2007-05-09 19:52:16 +0000 | [diff] [blame] | 2850 | if (i > 0) |
| 2851 | totalsize += mysize; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2852 | totalsize += Py_Size(obj); |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2853 | if (totalsize < 0) { |
| 2854 | PyErr_NoMemory(); |
| 2855 | goto error; |
| 2856 | } |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | /* Allocate the result, and copy the bytes */ |
| 2860 | result = PyBytes_FromStringAndSize(NULL, totalsize); |
| 2861 | if (result == NULL) |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2862 | goto error; |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2863 | dest = PyBytes_AS_STRING(result); |
| 2864 | for (i = 0; i < n; i++) { |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2865 | PyObject *obj = items[i]; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2866 | Py_ssize_t size = Py_Size(obj); |
| 2867 | char *buf; |
| 2868 | if (PyBytes_Check(obj)) |
| 2869 | buf = PyBytes_AS_STRING(obj); |
| 2870 | else |
| 2871 | buf = PyString_AS_STRING(obj); |
| 2872 | if (i) { |
Guido van Rossum | cd6ae68 | 2007-05-09 19:52:16 +0000 | [diff] [blame] | 2873 | memcpy(dest, self->ob_bytes, mysize); |
| 2874 | dest += mysize; |
| 2875 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2876 | memcpy(dest, buf, size); |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2877 | dest += size; |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | /* Done */ |
| 2881 | Py_DECREF(seq); |
| 2882 | return result; |
| 2883 | |
| 2884 | /* Error handling */ |
| 2885 | error: |
| 2886 | Py_DECREF(seq); |
| 2887 | return NULL; |
| 2888 | } |
| 2889 | |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2890 | PyDoc_STRVAR(fromhex_doc, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2891 | "buffer.fromhex(string) -> buffer\n\ |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2892 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2893 | Create a buffer object from a string of hexadecimal numbers.\n\ |
| 2894 | Spaces between two numbers are accepted.\n\ |
| 2895 | Example: buffer.fromhex('B9 01EF') -> buffer(b'\\xb9\\x01\\xef')."); |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2896 | |
| 2897 | static int |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2898 | hex_digit_to_int(Py_UNICODE c) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2899 | { |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2900 | if (c >= 128) |
| 2901 | return -1; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 2902 | if (ISDIGIT(c)) |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2903 | return c - '0'; |
| 2904 | else { |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 2905 | if (ISUPPER(c)) |
| 2906 | c = TOLOWER(c); |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 2907 | if (c >= 'a' && c <= 'f') |
| 2908 | return c - 'a' + 10; |
| 2909 | } |
| 2910 | return -1; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | static PyObject * |
| 2914 | bytes_fromhex(PyObject *cls, PyObject *args) |
| 2915 | { |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 2916 | PyObject *newbytes, *hexobj; |
| 2917 | char *buf; |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2918 | Py_UNICODE *hex; |
| 2919 | Py_ssize_t hexlen, byteslen, i, j; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2920 | int top, bot; |
| 2921 | |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2922 | if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2923 | return NULL; |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2924 | assert(PyUnicode_Check(hexobj)); |
| 2925 | hexlen = PyUnicode_GET_SIZE(hexobj); |
| 2926 | hex = PyUnicode_AS_UNICODE(hexobj); |
| 2927 | byteslen = hexlen/2; /* This overestimates if there are spaces */ |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2928 | newbytes = PyBytes_FromStringAndSize(NULL, byteslen); |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2929 | if (!newbytes) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2930 | return NULL; |
| 2931 | buf = PyBytes_AS_STRING(newbytes); |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2932 | for (i = j = 0; i < hexlen; i += 2) { |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2933 | /* skip over spaces in the input */ |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2934 | while (hex[i] == ' ') |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2935 | i++; |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2936 | if (i >= hexlen) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2937 | break; |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2938 | top = hex_digit_to_int(hex[i]); |
| 2939 | bot = hex_digit_to_int(hex[i+1]); |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2940 | if (top == -1 || bot == -1) { |
| 2941 | PyErr_Format(PyExc_ValueError, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2942 | "non-hexadecimal number found in " |
| 2943 | "fromhex() arg at position %zd", i); |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2944 | goto error; |
| 2945 | } |
| 2946 | buf[j++] = (top << 4) + bot; |
| 2947 | } |
| 2948 | if (PyBytes_Resize(newbytes, j) < 0) |
| 2949 | goto error; |
| 2950 | return newbytes; |
| 2951 | |
| 2952 | error: |
| 2953 | Py_DECREF(newbytes); |
| 2954 | return NULL; |
| 2955 | } |
| 2956 | |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 2957 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 2958 | |
| 2959 | static PyObject * |
| 2960 | bytes_reduce(PyBytesObject *self) |
| 2961 | { |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 2962 | PyObject *latin1, *dict; |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 2963 | if (self->ob_bytes) |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 2964 | latin1 = PyUnicode_DecodeLatin1(self->ob_bytes, |
| 2965 | Py_Size(self), NULL); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 2966 | else |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 2967 | latin1 = PyUnicode_FromString(""); |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 2968 | |
| 2969 | dict = PyObject_GetAttrString((PyObject *)self, "__dict__"); |
| 2970 | if (dict == NULL) { |
| 2971 | PyErr_Clear(); |
| 2972 | dict = Py_None; |
| 2973 | Py_INCREF(dict); |
| 2974 | } |
| 2975 | |
| 2976 | 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] | 2977 | } |
| 2978 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2979 | static PySequenceMethods bytes_as_sequence = { |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2980 | (lenfunc)bytes_length, /* sq_length */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 2981 | (binaryfunc)PyBytes_Concat, /* sq_concat */ |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2982 | (ssizeargfunc)bytes_repeat, /* sq_repeat */ |
| 2983 | (ssizeargfunc)bytes_getitem, /* sq_item */ |
| 2984 | 0, /* sq_slice */ |
| 2985 | (ssizeobjargproc)bytes_setitem, /* sq_ass_item */ |
| 2986 | 0, /* sq_ass_slice */ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2987 | (objobjproc)bytes_contains, /* sq_contains */ |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2988 | (binaryfunc)bytes_iconcat, /* sq_inplace_concat */ |
| 2989 | (ssizeargfunc)bytes_irepeat, /* sq_inplace_repeat */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2990 | }; |
| 2991 | |
| 2992 | static PyMappingMethods bytes_as_mapping = { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2993 | (lenfunc)bytes_length, |
Thomas Wouters | 376446d | 2006-12-19 08:30:14 +0000 | [diff] [blame] | 2994 | (binaryfunc)bytes_subscript, |
| 2995 | (objobjargproc)bytes_ass_subscript, |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2996 | }; |
| 2997 | |
| 2998 | static PyBufferProcs bytes_as_buffer = { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2999 | (getbufferproc)bytes_getbuffer, |
| 3000 | (releasebufferproc)bytes_releasebuffer, |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3001 | }; |
| 3002 | |
| 3003 | static PyMethodDef |
| 3004 | bytes_methods[] = { |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 3005 | {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc}, |
| 3006 | {"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc}, |
| 3007 | {"append", (PyCFunction)bytes_append, METH_O, append__doc__}, |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 3008 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 3009 | _Py_capitalize__doc__}, |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 3010 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 3011 | {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__}, |
| 3012 | {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc}, |
| 3013 | {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, endswith__doc__}, |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 3014 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, |
| 3015 | expandtabs__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 3016 | {"extend", (PyCFunction)bytes_extend, METH_O, extend__doc__}, |
| 3017 | {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__}, |
| 3018 | {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS, |
| 3019 | fromhex_doc}, |
| 3020 | {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__}, |
| 3021 | {"insert", (PyCFunction)bytes_insert, METH_VARARGS, insert__doc__}, |
| 3022 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 3023 | _Py_isalnum__doc__}, |
| 3024 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 3025 | _Py_isalpha__doc__}, |
| 3026 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 3027 | _Py_isdigit__doc__}, |
| 3028 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 3029 | _Py_islower__doc__}, |
| 3030 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 3031 | _Py_isspace__doc__}, |
| 3032 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 3033 | _Py_istitle__doc__}, |
| 3034 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 3035 | _Py_isupper__doc__}, |
| 3036 | {"join", (PyCFunction)bytes_join, METH_O, join_doc}, |
| 3037 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 3038 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
| 3039 | {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__}, |
| 3040 | {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__}, |
| 3041 | {"pop", (PyCFunction)bytes_pop, METH_VARARGS, pop__doc__}, |
| 3042 | {"remove", (PyCFunction)bytes_remove, METH_O, remove__doc__}, |
| 3043 | {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__}, |
| 3044 | {"reverse", (PyCFunction)bytes_reverse, METH_NOARGS, reverse__doc__}, |
| 3045 | {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__}, |
| 3046 | {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__}, |
| 3047 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
| 3048 | {"rpartition", (PyCFunction)bytes_rpartition, METH_O, rpartition__doc__}, |
| 3049 | {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__}, |
| 3050 | {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__}, |
| 3051 | {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__}, |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 3052 | {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS, |
| 3053 | splitlines__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 3054 | {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS , |
| 3055 | startswith__doc__}, |
| 3056 | {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__}, |
| 3057 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 3058 | _Py_swapcase__doc__}, |
| 3059 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
| 3060 | {"translate", (PyCFunction)bytes_translate, METH_VARARGS, |
| 3061 | translate__doc__}, |
| 3062 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
| 3063 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame] | 3064 | {NULL} |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3065 | }; |
| 3066 | |
| 3067 | PyDoc_STRVAR(bytes_doc, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 3068 | "buffer(iterable_of_ints) -> buffer.\n\ |
| 3069 | buffer(string, encoding[, errors]) -> buffer.\n\ |
| 3070 | buffer(bytes_or_buffer) -> mutable copy of bytes_or_buffer.\n\ |
| 3071 | buffer(memory_view) -> buffer.\n\ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3072 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 3073 | Construct an mutable buffer object from:\n\ |
| 3074 | - an iterable yielding integers in range(256)\n\ |
| 3075 | - a text string encoded using the specified encoding\n\ |
| 3076 | - a bytes or a buffer object\n\ |
| 3077 | - any object implementing the buffer API.\n\ |
| 3078 | \n\ |
| 3079 | buffer(int) -> buffer.\n\ |
| 3080 | \n\ |
| 3081 | Construct a zero-initialized buffer of the given length."); |
| 3082 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3083 | |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3084 | static PyObject *bytes_iter(PyObject *seq); |
| 3085 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3086 | PyTypeObject PyBytes_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3087 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 3088 | "buffer", |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3089 | sizeof(PyBytesObject), |
| 3090 | 0, |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 3091 | (destructor)bytes_dealloc, /* tp_dealloc */ |
| 3092 | 0, /* tp_print */ |
| 3093 | 0, /* tp_getattr */ |
| 3094 | 0, /* tp_setattr */ |
| 3095 | 0, /* tp_compare */ |
| 3096 | (reprfunc)bytes_repr, /* tp_repr */ |
| 3097 | 0, /* tp_as_number */ |
| 3098 | &bytes_as_sequence, /* tp_as_sequence */ |
| 3099 | &bytes_as_mapping, /* tp_as_mapping */ |
Georg Brandl | b3f568f | 2007-02-27 08:49:18 +0000 | [diff] [blame] | 3100 | 0, /* tp_hash */ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 3101 | 0, /* tp_call */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 3102 | bytes_str, /* tp_str */ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 3103 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3104 | 0, /* tp_setattro */ |
| 3105 | &bytes_as_buffer, /* tp_as_buffer */ |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 3106 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 3107 | bytes_doc, /* tp_doc */ |
| 3108 | 0, /* tp_traverse */ |
| 3109 | 0, /* tp_clear */ |
| 3110 | (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
| 3111 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3112 | bytes_iter, /* tp_iter */ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 3113 | 0, /* tp_iternext */ |
| 3114 | bytes_methods, /* tp_methods */ |
| 3115 | 0, /* tp_members */ |
| 3116 | 0, /* tp_getset */ |
| 3117 | 0, /* tp_base */ |
| 3118 | 0, /* tp_dict */ |
| 3119 | 0, /* tp_descr_get */ |
| 3120 | 0, /* tp_descr_set */ |
| 3121 | 0, /* tp_dictoffset */ |
| 3122 | (initproc)bytes_init, /* tp_init */ |
| 3123 | PyType_GenericAlloc, /* tp_alloc */ |
| 3124 | PyType_GenericNew, /* tp_new */ |
| 3125 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3126 | }; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3127 | |
| 3128 | /*********************** Bytes Iterator ****************************/ |
| 3129 | |
| 3130 | typedef struct { |
| 3131 | PyObject_HEAD |
| 3132 | Py_ssize_t it_index; |
| 3133 | PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 3134 | } bytesiterobject; |
| 3135 | |
| 3136 | static void |
| 3137 | bytesiter_dealloc(bytesiterobject *it) |
| 3138 | { |
| 3139 | _PyObject_GC_UNTRACK(it); |
| 3140 | Py_XDECREF(it->it_seq); |
| 3141 | PyObject_GC_Del(it); |
| 3142 | } |
| 3143 | |
| 3144 | static int |
| 3145 | bytesiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
| 3146 | { |
| 3147 | Py_VISIT(it->it_seq); |
| 3148 | return 0; |
| 3149 | } |
| 3150 | |
| 3151 | static PyObject * |
| 3152 | bytesiter_next(bytesiterobject *it) |
| 3153 | { |
| 3154 | PyBytesObject *seq; |
| 3155 | PyObject *item; |
| 3156 | |
| 3157 | assert(it != NULL); |
| 3158 | seq = it->it_seq; |
| 3159 | if (seq == NULL) |
| 3160 | return NULL; |
| 3161 | assert(PyBytes_Check(seq)); |
| 3162 | |
| 3163 | if (it->it_index < PyBytes_GET_SIZE(seq)) { |
| 3164 | item = PyInt_FromLong( |
| 3165 | (unsigned char)seq->ob_bytes[it->it_index]); |
| 3166 | if (item != NULL) |
| 3167 | ++it->it_index; |
| 3168 | return item; |
| 3169 | } |
| 3170 | |
| 3171 | Py_DECREF(seq); |
| 3172 | it->it_seq = NULL; |
| 3173 | return NULL; |
| 3174 | } |
| 3175 | |
| 3176 | static PyObject * |
| 3177 | bytesiter_length_hint(bytesiterobject *it) |
| 3178 | { |
| 3179 | Py_ssize_t len = 0; |
| 3180 | if (it->it_seq) |
| 3181 | len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
| 3182 | return PyInt_FromSsize_t(len); |
| 3183 | } |
| 3184 | |
| 3185 | PyDoc_STRVAR(length_hint_doc, |
| 3186 | "Private method returning an estimate of len(list(it))."); |
| 3187 | |
| 3188 | static PyMethodDef bytesiter_methods[] = { |
| 3189 | {"__length_hint__", (PyCFunction)bytesiter_length_hint, METH_NOARGS, |
| 3190 | length_hint_doc}, |
| 3191 | {NULL, NULL} /* sentinel */ |
| 3192 | }; |
| 3193 | |
| 3194 | PyTypeObject PyBytesIter_Type = { |
| 3195 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3196 | "bytesiterator", /* tp_name */ |
| 3197 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 3198 | 0, /* tp_itemsize */ |
| 3199 | /* methods */ |
| 3200 | (destructor)bytesiter_dealloc, /* tp_dealloc */ |
| 3201 | 0, /* tp_print */ |
| 3202 | 0, /* tp_getattr */ |
| 3203 | 0, /* tp_setattr */ |
| 3204 | 0, /* tp_compare */ |
| 3205 | 0, /* tp_repr */ |
| 3206 | 0, /* tp_as_number */ |
| 3207 | 0, /* tp_as_sequence */ |
| 3208 | 0, /* tp_as_mapping */ |
| 3209 | 0, /* tp_hash */ |
| 3210 | 0, /* tp_call */ |
| 3211 | 0, /* tp_str */ |
| 3212 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3213 | 0, /* tp_setattro */ |
| 3214 | 0, /* tp_as_buffer */ |
| 3215 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 3216 | 0, /* tp_doc */ |
| 3217 | (traverseproc)bytesiter_traverse, /* tp_traverse */ |
| 3218 | 0, /* tp_clear */ |
| 3219 | 0, /* tp_richcompare */ |
| 3220 | 0, /* tp_weaklistoffset */ |
| 3221 | PyObject_SelfIter, /* tp_iter */ |
| 3222 | (iternextfunc)bytesiter_next, /* tp_iternext */ |
| 3223 | bytesiter_methods, /* tp_methods */ |
| 3224 | 0, |
| 3225 | }; |
| 3226 | |
| 3227 | static PyObject * |
| 3228 | bytes_iter(PyObject *seq) |
| 3229 | { |
| 3230 | bytesiterobject *it; |
| 3231 | |
| 3232 | if (!PyBytes_Check(seq)) { |
| 3233 | PyErr_BadInternalCall(); |
| 3234 | return NULL; |
| 3235 | } |
| 3236 | it = PyObject_GC_New(bytesiterobject, &PyBytesIter_Type); |
| 3237 | if (it == NULL) |
| 3238 | return NULL; |
| 3239 | it->it_index = 0; |
| 3240 | Py_INCREF(seq); |
| 3241 | it->it_seq = (PyBytesObject *)seq; |
| 3242 | _PyObject_GC_TRACK(it); |
| 3243 | return (PyObject *)it; |
| 3244 | } |