Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 1 | /* bytes object implementation */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3 | #define PY_SSIZE_T_CLEAN |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 6 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 7 | #include "bytes_methods.h" |
Mark Dickinson | fd24b32 | 2008-12-06 15:33:31 +0000 | [diff] [blame] | 8 | #include <stddef.h> |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 9 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 10 | /*[clinic input] |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 11 | class bytes "PyBytesObject*" "&PyBytes_Type" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 12 | [clinic start generated code]*/ |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 13 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=1a1d9102afc1b00c]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 14 | |
Neal Norwitz | 2bad970 | 2007-08-27 06:19:22 +0000 | [diff] [blame] | 15 | static Py_ssize_t |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 16 | _getbuffer(PyObject *obj, Py_buffer *view) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 17 | { |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 18 | PyBufferProcs *bufferprocs; |
| 19 | if (PyBytes_CheckExact(obj)) { |
| 20 | /* Fast path, e.g. for .join() of many bytes objects */ |
| 21 | Py_INCREF(obj); |
| 22 | view->obj = obj; |
| 23 | view->buf = PyBytes_AS_STRING(obj); |
| 24 | view->len = PyBytes_GET_SIZE(obj); |
| 25 | return view->len; |
| 26 | } |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 27 | |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 28 | bufferprocs = Py_TYPE(obj)->tp_as_buffer; |
| 29 | if (bufferprocs == NULL || bufferprocs->bf_getbuffer == NULL) |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 30 | { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 31 | PyErr_Format(PyExc_TypeError, |
R David Murray | 861470c | 2014-10-05 11:47:01 -0400 | [diff] [blame^] | 32 | "a bytes-like object is required, not '%.100s'", |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 33 | Py_TYPE(obj)->tp_name); |
| 34 | return -1; |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 35 | } |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 36 | |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 37 | if (bufferprocs->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | return -1; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 39 | return view->len; |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 42 | #ifdef COUNT_ALLOCS |
Benjamin Peterson | a4a37fe | 2009-01-11 17:13:55 +0000 | [diff] [blame] | 43 | Py_ssize_t null_strings, one_strings; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 44 | #endif |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 45 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 46 | static PyBytesObject *characters[UCHAR_MAX + 1]; |
| 47 | static PyBytesObject *nullstring; |
| 48 | |
Mark Dickinson | fd24b32 | 2008-12-06 15:33:31 +0000 | [diff] [blame] | 49 | /* PyBytesObject_SIZE gives the basic size of a string; any memory allocation |
| 50 | for a string of length n should request PyBytesObject_SIZE + n bytes. |
| 51 | |
| 52 | Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves |
| 53 | 3 bytes per string allocation on a typical system. |
| 54 | */ |
| 55 | #define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1) |
| 56 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 57 | /* |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 58 | For PyBytes_FromString(), the parameter `str' points to a null-terminated |
| 59 | string containing exactly `size' bytes. |
| 60 | |
| 61 | For PyBytes_FromStringAndSize(), the parameter the parameter `str' is |
| 62 | either NULL or else points to a string containing at least `size' bytes. |
| 63 | For PyBytes_FromStringAndSize(), the string in the `str' parameter does |
| 64 | not have to be null-terminated. (Therefore it is safe to construct a |
| 65 | substring by calling `PyBytes_FromStringAndSize(origstring, substrlen)'.) |
| 66 | If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1' |
| 67 | bytes (setting the last byte to the null terminating character) and you can |
| 68 | fill in the data yourself. If `str' is non-NULL then the resulting |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 69 | PyBytes object must be treated as immutable and you must not fill in nor |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 70 | alter the data yourself, since the strings may be shared. |
| 71 | |
| 72 | The PyObject member `op->ob_size', which denotes the number of "extra |
| 73 | items" in a variable-size object, will contain the number of bytes |
Eli Bendersky | 1aef6b6 | 2011-03-24 22:32:56 +0200 | [diff] [blame] | 74 | allocated for string data, not counting the null terminating character. |
| 75 | It is therefore equal to the `size' parameter (for |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 76 | PyBytes_FromStringAndSize()) or the length of the string in the `str' |
| 77 | parameter (for PyBytes_FromString()). |
| 78 | */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 79 | static PyObject * |
| 80 | _PyBytes_FromSize(Py_ssize_t size, int use_calloc) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 81 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 82 | PyBytesObject *op; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 83 | assert(size >= 0); |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 84 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | if (size == 0 && (op = nullstring) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 86 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | null_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 88 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | Py_INCREF(op); |
| 90 | return (PyObject *)op; |
| 91 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 92 | |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 93 | if ((size_t)size > (size_t)PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | PyErr_SetString(PyExc_OverflowError, |
| 95 | "byte string is too large"); |
| 96 | return NULL; |
| 97 | } |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 98 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | /* Inline PyObject_NewVar */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 100 | if (use_calloc) |
| 101 | op = (PyBytesObject *)PyObject_Calloc(1, PyBytesObject_SIZE + size); |
| 102 | else |
| 103 | op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | if (op == NULL) |
| 105 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 106 | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | op->ob_shash = -1; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 108 | if (!use_calloc) |
| 109 | op->ob_sval[size] = '\0'; |
| 110 | /* empty byte string singleton */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | if (size == 0) { |
| 112 | nullstring = op; |
| 113 | Py_INCREF(op); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 114 | } |
| 115 | return (PyObject *) op; |
| 116 | } |
| 117 | |
| 118 | PyObject * |
| 119 | PyBytes_FromStringAndSize(const char *str, Py_ssize_t size) |
| 120 | { |
| 121 | PyBytesObject *op; |
| 122 | if (size < 0) { |
| 123 | PyErr_SetString(PyExc_SystemError, |
| 124 | "Negative size passed to PyBytes_FromStringAndSize"); |
| 125 | return NULL; |
| 126 | } |
| 127 | if (size == 1 && str != NULL && |
| 128 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 129 | { |
| 130 | #ifdef COUNT_ALLOCS |
| 131 | one_strings++; |
| 132 | #endif |
| 133 | Py_INCREF(op); |
| 134 | return (PyObject *)op; |
| 135 | } |
| 136 | |
| 137 | op = (PyBytesObject *)_PyBytes_FromSize(size, 0); |
| 138 | if (op == NULL) |
| 139 | return NULL; |
| 140 | if (str == NULL) |
| 141 | return (PyObject *) op; |
| 142 | |
| 143 | Py_MEMCPY(op->ob_sval, str, size); |
| 144 | /* share short strings */ |
| 145 | if (size == 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | characters[*str & UCHAR_MAX] = op; |
| 147 | Py_INCREF(op); |
| 148 | } |
| 149 | return (PyObject *) op; |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 152 | PyObject * |
| 153 | PyBytes_FromString(const char *str) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 154 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 155 | size_t size; |
| 156 | PyBytesObject *op; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | assert(str != NULL); |
| 159 | size = strlen(str); |
| 160 | if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
| 161 | PyErr_SetString(PyExc_OverflowError, |
| 162 | "byte string is too long"); |
| 163 | return NULL; |
| 164 | } |
| 165 | if (size == 0 && (op = nullstring) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 166 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | null_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 168 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | Py_INCREF(op); |
| 170 | return (PyObject *)op; |
| 171 | } |
| 172 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 173 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | one_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 175 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | Py_INCREF(op); |
| 177 | return (PyObject *)op; |
| 178 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | /* Inline PyObject_NewVar */ |
| 181 | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
| 182 | if (op == NULL) |
| 183 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 184 | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | op->ob_shash = -1; |
| 186 | Py_MEMCPY(op->ob_sval, str, size+1); |
| 187 | /* share short strings */ |
| 188 | if (size == 0) { |
| 189 | nullstring = op; |
| 190 | Py_INCREF(op); |
| 191 | } else if (size == 1) { |
| 192 | characters[*str & UCHAR_MAX] = op; |
| 193 | Py_INCREF(op); |
| 194 | } |
| 195 | return (PyObject *) op; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 196 | } |
Guido van Rossum | ebea9be | 2007-04-09 00:49:13 +0000 | [diff] [blame] | 197 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 198 | PyObject * |
| 199 | PyBytes_FromFormatV(const char *format, va_list vargs) |
| 200 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | va_list count; |
| 202 | Py_ssize_t n = 0; |
| 203 | const char* f; |
| 204 | char *s; |
| 205 | PyObject* string; |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 206 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 207 | Py_VA_COPY(count, vargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | /* step 1: figure out how large a buffer we need */ |
| 209 | for (f = format; *f; f++) { |
| 210 | if (*f == '%') { |
| 211 | const char* p = f; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 212 | while (*++f && *f != '%' && !Py_ISALPHA(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | ; |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 216 | * they don't affect the amount of space we reserve. |
| 217 | */ |
| 218 | if ((*f == 'l' || *f == 'z') && |
| 219 | (f[1] == 'd' || f[1] == 'u')) |
| 220 | ++f; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | switch (*f) { |
| 223 | case 'c': |
Victor Stinner | c9362cf | 2013-12-13 12:14:44 +0100 | [diff] [blame] | 224 | { |
| 225 | int c = va_arg(count, int); |
| 226 | if (c < 0 || c > 255) { |
| 227 | PyErr_SetString(PyExc_OverflowError, |
| 228 | "PyBytes_FromFormatV(): %c format " |
| 229 | "expects an integer in range [0; 255]"); |
| 230 | return NULL; |
| 231 | } |
| 232 | n++; |
| 233 | break; |
| 234 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | case '%': |
| 236 | n++; |
| 237 | break; |
| 238 | case 'd': case 'u': case 'i': case 'x': |
| 239 | (void) va_arg(count, int); |
| 240 | /* 20 bytes is enough to hold a 64-bit |
| 241 | integer. Decimal takes the most space. |
| 242 | This isn't enough for octal. */ |
| 243 | n += 20; |
| 244 | break; |
| 245 | case 's': |
| 246 | s = va_arg(count, char*); |
| 247 | n += strlen(s); |
| 248 | break; |
| 249 | case 'p': |
| 250 | (void) va_arg(count, int); |
| 251 | /* maximum 64-bit pointer representation: |
| 252 | * 0xffffffffffffffff |
| 253 | * so 19 characters is enough. |
| 254 | * XXX I count 18 -- what's the extra for? |
| 255 | */ |
| 256 | n += 19; |
| 257 | break; |
| 258 | default: |
| 259 | /* if we stumble upon an unknown |
| 260 | formatting code, copy the rest of |
| 261 | the format string to the output |
| 262 | string. (we cannot just skip the |
| 263 | code, since there's no way to know |
| 264 | what's in the argument list) */ |
| 265 | n += strlen(p); |
| 266 | goto expand; |
| 267 | } |
| 268 | } else |
| 269 | n++; |
| 270 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 271 | expand: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | /* step 2: fill the buffer */ |
| 273 | /* Since we've analyzed how much space we need for the worst case, |
| 274 | use sprintf directly instead of the slower PyOS_snprintf. */ |
| 275 | string = PyBytes_FromStringAndSize(NULL, n); |
| 276 | if (!string) |
| 277 | return NULL; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | s = PyBytes_AsString(string); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | for (f = format; *f; f++) { |
| 282 | if (*f == '%') { |
| 283 | const char* p = f++; |
| 284 | Py_ssize_t i; |
| 285 | int longflag = 0; |
| 286 | int size_tflag = 0; |
| 287 | /* parse the width.precision part (we're only |
| 288 | interested in the precision value, if any) */ |
| 289 | n = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 290 | while (Py_ISDIGIT(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | n = (n*10) + *f++ - '0'; |
| 292 | if (*f == '.') { |
| 293 | f++; |
| 294 | n = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 295 | while (Py_ISDIGIT(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | n = (n*10) + *f++ - '0'; |
| 297 | } |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 298 | while (*f && *f != '%' && !Py_ISALPHA(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | f++; |
| 300 | /* handle the long flag, but only for %ld and %lu. |
| 301 | others can be added when necessary. */ |
| 302 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 303 | longflag = 1; |
| 304 | ++f; |
| 305 | } |
| 306 | /* handle the size_t flag. */ |
| 307 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 308 | size_tflag = 1; |
| 309 | ++f; |
| 310 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | switch (*f) { |
| 313 | case 'c': |
Victor Stinner | c9362cf | 2013-12-13 12:14:44 +0100 | [diff] [blame] | 314 | { |
| 315 | int c = va_arg(vargs, int); |
| 316 | /* c has been checked for overflow in the first step */ |
| 317 | *s++ = (unsigned char)c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | break; |
Victor Stinner | c9362cf | 2013-12-13 12:14:44 +0100 | [diff] [blame] | 319 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | case 'd': |
| 321 | if (longflag) |
| 322 | sprintf(s, "%ld", va_arg(vargs, long)); |
| 323 | else if (size_tflag) |
| 324 | sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
| 325 | va_arg(vargs, Py_ssize_t)); |
| 326 | else |
| 327 | sprintf(s, "%d", va_arg(vargs, int)); |
| 328 | s += strlen(s); |
| 329 | break; |
| 330 | case 'u': |
| 331 | if (longflag) |
| 332 | sprintf(s, "%lu", |
| 333 | va_arg(vargs, unsigned long)); |
| 334 | else if (size_tflag) |
| 335 | sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
| 336 | va_arg(vargs, size_t)); |
| 337 | else |
| 338 | sprintf(s, "%u", |
| 339 | va_arg(vargs, unsigned int)); |
| 340 | s += strlen(s); |
| 341 | break; |
| 342 | case 'i': |
| 343 | sprintf(s, "%i", va_arg(vargs, int)); |
| 344 | s += strlen(s); |
| 345 | break; |
| 346 | case 'x': |
| 347 | sprintf(s, "%x", va_arg(vargs, int)); |
| 348 | s += strlen(s); |
| 349 | break; |
| 350 | case 's': |
| 351 | p = va_arg(vargs, char*); |
| 352 | i = strlen(p); |
| 353 | if (n > 0 && i > n) |
| 354 | i = n; |
| 355 | Py_MEMCPY(s, p, i); |
| 356 | s += i; |
| 357 | break; |
| 358 | case 'p': |
| 359 | sprintf(s, "%p", va_arg(vargs, void*)); |
| 360 | /* %p is ill-defined: ensure leading 0x. */ |
| 361 | if (s[1] == 'X') |
| 362 | s[1] = 'x'; |
| 363 | else if (s[1] != 'x') { |
| 364 | memmove(s+2, s, strlen(s)+1); |
| 365 | s[0] = '0'; |
| 366 | s[1] = 'x'; |
| 367 | } |
| 368 | s += strlen(s); |
| 369 | break; |
| 370 | case '%': |
| 371 | *s++ = '%'; |
| 372 | break; |
| 373 | default: |
| 374 | strcpy(s, p); |
| 375 | s += strlen(s); |
| 376 | goto end; |
| 377 | } |
| 378 | } else |
| 379 | *s++ = *f; |
| 380 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 381 | |
| 382 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | _PyBytes_Resize(&string, s - PyBytes_AS_STRING(string)); |
| 384 | return string; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | PyObject * |
| 388 | PyBytes_FromFormat(const char *format, ...) |
| 389 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | PyObject* ret; |
| 391 | va_list vargs; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 392 | |
| 393 | #ifdef HAVE_STDARG_PROTOTYPES |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | va_start(vargs, format); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 395 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | va_start(vargs); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 397 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | ret = PyBytes_FromFormatV(format, vargs); |
| 399 | va_end(vargs); |
| 400 | return ret; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static void |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 404 | bytes_dealloc(PyObject *op) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 405 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | Py_TYPE(op)->tp_free(op); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 409 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 410 | the string is a u-literal. If recode_encoding is non-zero, |
| 411 | the string is UTF-8 encoded and should be re-encoded in the |
| 412 | specified encoding. */ |
| 413 | |
| 414 | PyObject *PyBytes_DecodeEscape(const char *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | Py_ssize_t len, |
| 416 | const char *errors, |
| 417 | Py_ssize_t unicode, |
| 418 | const char *recode_encoding) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 419 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | int c; |
| 421 | char *p, *buf; |
| 422 | const char *end; |
| 423 | PyObject *v; |
| 424 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
| 425 | v = PyBytes_FromStringAndSize((char *)NULL, newlen); |
| 426 | if (v == NULL) |
| 427 | return NULL; |
| 428 | p = buf = PyBytes_AsString(v); |
| 429 | end = s + len; |
| 430 | while (s < end) { |
| 431 | if (*s != '\\') { |
| 432 | non_esc: |
| 433 | if (recode_encoding && (*s & 0x80)) { |
| 434 | PyObject *u, *w; |
| 435 | char *r; |
| 436 | const char* t; |
| 437 | Py_ssize_t rn; |
| 438 | t = s; |
| 439 | /* Decode non-ASCII bytes as UTF-8. */ |
| 440 | while (t < end && (*t & 0x80)) t++; |
| 441 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
| 442 | if(!u) goto failed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | /* Recode them in target encoding. */ |
| 445 | w = PyUnicode_AsEncodedString( |
| 446 | u, recode_encoding, errors); |
| 447 | Py_DECREF(u); |
| 448 | if (!w) goto failed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | /* Append bytes to output buffer. */ |
| 451 | assert(PyBytes_Check(w)); |
| 452 | r = PyBytes_AS_STRING(w); |
| 453 | rn = PyBytes_GET_SIZE(w); |
| 454 | Py_MEMCPY(p, r, rn); |
| 455 | p += rn; |
| 456 | Py_DECREF(w); |
| 457 | s = t; |
| 458 | } else { |
| 459 | *p++ = *s++; |
| 460 | } |
| 461 | continue; |
| 462 | } |
| 463 | s++; |
| 464 | if (s==end) { |
| 465 | PyErr_SetString(PyExc_ValueError, |
| 466 | "Trailing \\ in string"); |
| 467 | goto failed; |
| 468 | } |
| 469 | switch (*s++) { |
| 470 | /* XXX This assumes ASCII! */ |
| 471 | case '\n': break; |
| 472 | case '\\': *p++ = '\\'; break; |
| 473 | case '\'': *p++ = '\''; break; |
| 474 | case '\"': *p++ = '\"'; break; |
| 475 | case 'b': *p++ = '\b'; break; |
| 476 | case 'f': *p++ = '\014'; break; /* FF */ |
| 477 | case 't': *p++ = '\t'; break; |
| 478 | case 'n': *p++ = '\n'; break; |
| 479 | case 'r': *p++ = '\r'; break; |
| 480 | case 'v': *p++ = '\013'; break; /* VT */ |
| 481 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 482 | case '0': case '1': case '2': case '3': |
| 483 | case '4': case '5': case '6': case '7': |
| 484 | c = s[-1] - '0'; |
| 485 | if (s < end && '0' <= *s && *s <= '7') { |
| 486 | c = (c<<3) + *s++ - '0'; |
| 487 | if (s < end && '0' <= *s && *s <= '7') |
| 488 | c = (c<<3) + *s++ - '0'; |
| 489 | } |
| 490 | *p++ = c; |
| 491 | break; |
| 492 | case 'x': |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 493 | if (s+1 < end && Py_ISXDIGIT(s[0]) && Py_ISXDIGIT(s[1])) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | unsigned int x = 0; |
| 495 | c = Py_CHARMASK(*s); |
| 496 | s++; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 497 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | x = c - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 499 | else if (Py_ISLOWER(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | x = 10 + c - 'a'; |
| 501 | else |
| 502 | x = 10 + c - 'A'; |
| 503 | x = x << 4; |
| 504 | c = Py_CHARMASK(*s); |
| 505 | s++; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 506 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | x += c - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 508 | else if (Py_ISLOWER(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | x += 10 + c - 'a'; |
| 510 | else |
| 511 | x += 10 + c - 'A'; |
| 512 | *p++ = x; |
| 513 | break; |
| 514 | } |
| 515 | if (!errors || strcmp(errors, "strict") == 0) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 516 | PyErr_Format(PyExc_ValueError, |
| 517 | "invalid \\x escape at position %d", |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 518 | s - 2 - (end - len)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | goto failed; |
| 520 | } |
| 521 | if (strcmp(errors, "replace") == 0) { |
| 522 | *p++ = '?'; |
| 523 | } else if (strcmp(errors, "ignore") == 0) |
| 524 | /* do nothing */; |
| 525 | else { |
| 526 | PyErr_Format(PyExc_ValueError, |
| 527 | "decoding error; unknown " |
| 528 | "error handling code: %.400s", |
| 529 | errors); |
| 530 | goto failed; |
| 531 | } |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 532 | /* skip \x */ |
| 533 | if (s < end && Py_ISXDIGIT(s[0])) |
| 534 | s++; /* and a hexdigit */ |
| 535 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | default: |
| 537 | *p++ = '\\'; |
| 538 | s--; |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 539 | goto non_esc; /* an arbitrary number of unescaped |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | UTF-8 bytes may follow. */ |
| 541 | } |
| 542 | } |
| 543 | if (p-buf < newlen) |
| 544 | _PyBytes_Resize(&v, p - buf); |
| 545 | return v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 546 | failed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | Py_DECREF(v); |
| 548 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* -------------------------------------------------------------------- */ |
| 552 | /* object api */ |
| 553 | |
| 554 | Py_ssize_t |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 555 | PyBytes_Size(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | if (!PyBytes_Check(op)) { |
| 558 | PyErr_Format(PyExc_TypeError, |
| 559 | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
| 560 | return -1; |
| 561 | } |
| 562 | return Py_SIZE(op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | char * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 566 | PyBytes_AsString(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 567 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | if (!PyBytes_Check(op)) { |
| 569 | PyErr_Format(PyExc_TypeError, |
| 570 | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
| 571 | return NULL; |
| 572 | } |
| 573 | return ((PyBytesObject *)op)->ob_sval; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | int |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 577 | PyBytes_AsStringAndSize(PyObject *obj, |
| 578 | char **s, |
| 579 | Py_ssize_t *len) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | if (s == NULL) { |
| 582 | PyErr_BadInternalCall(); |
| 583 | return -1; |
| 584 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | if (!PyBytes_Check(obj)) { |
| 587 | PyErr_Format(PyExc_TypeError, |
| 588 | "expected bytes, %.200s found", Py_TYPE(obj)->tp_name); |
| 589 | return -1; |
| 590 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 | *s = PyBytes_AS_STRING(obj); |
| 593 | if (len != NULL) |
| 594 | *len = PyBytes_GET_SIZE(obj); |
| 595 | else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 596 | PyErr_SetString(PyExc_ValueError, |
| 597 | "embedded null byte"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | return -1; |
| 599 | } |
| 600 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 601 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 602 | |
| 603 | /* -------------------------------------------------------------------- */ |
| 604 | /* Methods */ |
| 605 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 606 | #include "stringlib/stringdefs.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 607 | |
| 608 | #include "stringlib/fastsearch.h" |
| 609 | #include "stringlib/count.h" |
| 610 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 611 | #include "stringlib/join.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 612 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 613 | #include "stringlib/split.h" |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 614 | #include "stringlib/ctype.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 615 | |
Eric Smith | 0f78bff | 2009-11-30 01:01:42 +0000 | [diff] [blame] | 616 | #include "stringlib/transmogrify.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 617 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 618 | PyObject * |
| 619 | PyBytes_Repr(PyObject *obj, int smartquotes) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 620 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 621 | PyBytesObject* op = (PyBytesObject*) obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 622 | Py_ssize_t i, length = Py_SIZE(op); |
Benjamin Peterson | d48bc94 | 2014-09-29 19:12:26 -0400 | [diff] [blame] | 623 | Py_ssize_t newsize, squotes, dquotes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 625 | unsigned char quote, *s, *p; |
| 626 | |
| 627 | /* Compute size of output string */ |
| 628 | squotes = dquotes = 0; |
| 629 | newsize = 3; /* b'' */ |
| 630 | s = (unsigned char*)op->ob_sval; |
| 631 | for (i = 0; i < length; i++) { |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 632 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 633 | switch(s[i]) { |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 634 | case '\'': squotes++; break; |
| 635 | case '"': dquotes++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 636 | case '\\': case '\t': case '\n': case '\r': |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 637 | incr = 2; break; /* \C */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 638 | default: |
| 639 | if (s[i] < ' ' || s[i] >= 0x7f) |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 640 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 641 | } |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 642 | if (newsize > PY_SSIZE_T_MAX - incr) |
| 643 | goto overflow; |
| 644 | newsize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 645 | } |
| 646 | quote = '\''; |
| 647 | if (smartquotes && squotes && !dquotes) |
| 648 | quote = '"'; |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 649 | if (squotes && quote == '\'') { |
| 650 | if (newsize > PY_SSIZE_T_MAX - squotes) |
| 651 | goto overflow; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 652 | newsize += squotes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 654 | |
| 655 | v = PyUnicode_New(newsize, 127); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | if (v == NULL) { |
| 657 | return NULL; |
| 658 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 659 | p = PyUnicode_1BYTE_DATA(v); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 660 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 661 | *p++ = 'b', *p++ = quote; |
| 662 | for (i = 0; i < length; i++) { |
| 663 | unsigned char c = op->ob_sval[i]; |
| 664 | if (c == quote || c == '\\') |
| 665 | *p++ = '\\', *p++ = c; |
| 666 | else if (c == '\t') |
| 667 | *p++ = '\\', *p++ = 't'; |
| 668 | else if (c == '\n') |
| 669 | *p++ = '\\', *p++ = 'n'; |
| 670 | else if (c == '\r') |
| 671 | *p++ = '\\', *p++ = 'r'; |
| 672 | else if (c < ' ' || c >= 0x7f) { |
| 673 | *p++ = '\\'; |
| 674 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 675 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 676 | *p++ = Py_hexdigits[c & 0xf]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 678 | else |
| 679 | *p++ = c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 681 | *p++ = quote; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 682 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 683 | return v; |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 684 | |
| 685 | overflow: |
| 686 | PyErr_SetString(PyExc_OverflowError, |
| 687 | "bytes object is too large to make repr"); |
| 688 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 691 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 692 | bytes_repr(PyObject *op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | return PyBytes_Repr(op, 1); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 697 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 698 | bytes_str(PyObject *op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | if (Py_BytesWarningFlag) { |
| 701 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 702 | "str() on a bytes instance", 1)) |
| 703 | return NULL; |
| 704 | } |
| 705 | return bytes_repr(op); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 708 | static Py_ssize_t |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 709 | bytes_length(PyBytesObject *a) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 710 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | return Py_SIZE(a); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 712 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 713 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 714 | /* This is also used by PyBytes_Concat() */ |
| 715 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 716 | bytes_concat(PyObject *a, PyObject *b) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | Py_ssize_t size; |
| 719 | Py_buffer va, vb; |
| 720 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | va.len = -1; |
| 723 | vb.len = -1; |
| 724 | if (_getbuffer(a, &va) < 0 || |
| 725 | _getbuffer(b, &vb) < 0) { |
| 726 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 727 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 728 | goto done; |
| 729 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | /* Optimize end cases */ |
| 732 | if (va.len == 0 && PyBytes_CheckExact(b)) { |
| 733 | result = b; |
| 734 | Py_INCREF(result); |
| 735 | goto done; |
| 736 | } |
| 737 | if (vb.len == 0 && PyBytes_CheckExact(a)) { |
| 738 | result = a; |
| 739 | Py_INCREF(result); |
| 740 | goto done; |
| 741 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | size = va.len + vb.len; |
| 744 | if (size < 0) { |
| 745 | PyErr_NoMemory(); |
| 746 | goto done; |
| 747 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | result = PyBytes_FromStringAndSize(NULL, size); |
| 750 | if (result != NULL) { |
| 751 | memcpy(PyBytes_AS_STRING(result), va.buf, va.len); |
| 752 | memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len); |
| 753 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 754 | |
| 755 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | if (va.len != -1) |
| 757 | PyBuffer_Release(&va); |
| 758 | if (vb.len != -1) |
| 759 | PyBuffer_Release(&vb); |
| 760 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 761 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 762 | |
| 763 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 764 | bytes_repeat(PyBytesObject *a, Py_ssize_t n) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 765 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 766 | Py_ssize_t i; |
| 767 | Py_ssize_t j; |
| 768 | Py_ssize_t size; |
| 769 | PyBytesObject *op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | size_t nbytes; |
| 771 | if (n < 0) |
| 772 | n = 0; |
| 773 | /* watch out for overflows: the size can overflow int, |
| 774 | * and the # of bytes needed can overflow size_t |
| 775 | */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 776 | if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | PyErr_SetString(PyExc_OverflowError, |
| 778 | "repeated bytes are too long"); |
| 779 | return NULL; |
| 780 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 781 | size = Py_SIZE(a) * n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) { |
| 783 | Py_INCREF(a); |
| 784 | return (PyObject *)a; |
| 785 | } |
| 786 | nbytes = (size_t)size; |
| 787 | if (nbytes + PyBytesObject_SIZE <= nbytes) { |
| 788 | PyErr_SetString(PyExc_OverflowError, |
| 789 | "repeated bytes are too long"); |
| 790 | return NULL; |
| 791 | } |
| 792 | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes); |
| 793 | if (op == NULL) |
| 794 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 795 | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | op->ob_shash = -1; |
| 797 | op->ob_sval[size] = '\0'; |
| 798 | if (Py_SIZE(a) == 1 && n > 0) { |
| 799 | memset(op->ob_sval, a->ob_sval[0] , n); |
| 800 | return (PyObject *) op; |
| 801 | } |
| 802 | i = 0; |
| 803 | if (i < size) { |
| 804 | Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
| 805 | i = Py_SIZE(a); |
| 806 | } |
| 807 | while (i < size) { |
| 808 | j = (i <= size-i) ? i : size-i; |
| 809 | Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
| 810 | i += j; |
| 811 | } |
| 812 | return (PyObject *) op; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 815 | static int |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 816 | bytes_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 817 | { |
| 818 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 819 | if (ival == -1 && PyErr_Occurred()) { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 820 | Py_buffer varg; |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 821 | Py_ssize_t pos; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 822 | PyErr_Clear(); |
| 823 | if (_getbuffer(arg, &varg) < 0) |
| 824 | return -1; |
| 825 | pos = stringlib_find(PyBytes_AS_STRING(self), Py_SIZE(self), |
| 826 | varg.buf, varg.len, 0); |
| 827 | PyBuffer_Release(&varg); |
| 828 | return pos >= 0; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 829 | } |
| 830 | if (ival < 0 || ival >= 256) { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 831 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 832 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 835 | return memchr(PyBytes_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 838 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 839 | bytes_item(PyBytesObject *a, Py_ssize_t i) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | if (i < 0 || i >= Py_SIZE(a)) { |
| 842 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 843 | return NULL; |
| 844 | } |
| 845 | return PyLong_FromLong((unsigned char)a->ob_sval[i]); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 848 | Py_LOCAL(int) |
| 849 | bytes_compare_eq(PyBytesObject *a, PyBytesObject *b) |
| 850 | { |
| 851 | int cmp; |
| 852 | Py_ssize_t len; |
| 853 | |
| 854 | len = Py_SIZE(a); |
| 855 | if (Py_SIZE(b) != len) |
| 856 | return 0; |
| 857 | |
| 858 | if (a->ob_sval[0] != b->ob_sval[0]) |
| 859 | return 0; |
| 860 | |
| 861 | cmp = memcmp(a->ob_sval, b->ob_sval, len); |
| 862 | return (cmp == 0); |
| 863 | } |
| 864 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 865 | static PyObject* |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 866 | bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 867 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | int c; |
| 869 | Py_ssize_t len_a, len_b; |
| 870 | Py_ssize_t min_len; |
| 871 | PyObject *result; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | /* Make sure both arguments are strings. */ |
| 874 | if (!(PyBytes_Check(a) && PyBytes_Check(b))) { |
| 875 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) && |
| 876 | (PyObject_IsInstance((PyObject*)a, |
| 877 | (PyObject*)&PyUnicode_Type) || |
| 878 | PyObject_IsInstance((PyObject*)b, |
| 879 | (PyObject*)&PyUnicode_Type))) { |
| 880 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 881 | "Comparison between bytes and string", 1)) |
| 882 | return NULL; |
| 883 | } |
| 884 | result = Py_NotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 886 | else if (a == b) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | switch (op) { |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 888 | case Py_EQ: |
| 889 | case Py_LE: |
| 890 | case Py_GE: |
| 891 | /* a string is equal to itself */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 892 | result = Py_True; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 893 | break; |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 894 | case Py_NE: |
| 895 | case Py_LT: |
| 896 | case Py_GT: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | result = Py_False; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 898 | break; |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 899 | default: |
| 900 | PyErr_BadArgument(); |
| 901 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | } |
| 903 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 904 | else if (op == Py_EQ || op == Py_NE) { |
| 905 | int eq = bytes_compare_eq(a, b); |
| 906 | eq ^= (op == Py_NE); |
| 907 | result = eq ? Py_True : Py_False; |
| 908 | } |
| 909 | else { |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 910 | len_a = Py_SIZE(a); |
| 911 | len_b = Py_SIZE(b); |
| 912 | min_len = Py_MIN(len_a, len_b); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 913 | if (min_len > 0) { |
| 914 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 915 | if (c == 0) |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 916 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 918 | else |
| 919 | c = 0; |
| 920 | if (c == 0) |
| 921 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 922 | switch (op) { |
| 923 | case Py_LT: c = c < 0; break; |
| 924 | case Py_LE: c = c <= 0; break; |
| 925 | case Py_GT: c = c > 0; break; |
| 926 | case Py_GE: c = c >= 0; break; |
| 927 | default: |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 928 | PyErr_BadArgument(); |
| 929 | return NULL; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 930 | } |
| 931 | result = c ? Py_True : Py_False; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | Py_INCREF(result); |
| 935 | return result; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 938 | static Py_hash_t |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 939 | bytes_hash(PyBytesObject *a) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 940 | { |
Antoine Pitrou | ce4a9da | 2011-11-21 20:46:33 +0100 | [diff] [blame] | 941 | if (a->ob_shash == -1) { |
| 942 | /* Can't fail */ |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 943 | a->ob_shash = _Py_HashBytes(a->ob_sval, Py_SIZE(a)); |
Antoine Pitrou | ce4a9da | 2011-11-21 20:46:33 +0100 | [diff] [blame] | 944 | } |
| 945 | return a->ob_shash; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 948 | static PyObject* |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 949 | bytes_subscript(PyBytesObject* self, PyObject* item) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 950 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 951 | if (PyIndex_Check(item)) { |
| 952 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 953 | if (i == -1 && PyErr_Occurred()) |
| 954 | return NULL; |
| 955 | if (i < 0) |
| 956 | i += PyBytes_GET_SIZE(self); |
| 957 | if (i < 0 || i >= PyBytes_GET_SIZE(self)) { |
| 958 | PyErr_SetString(PyExc_IndexError, |
| 959 | "index out of range"); |
| 960 | return NULL; |
| 961 | } |
| 962 | return PyLong_FromLong((unsigned char)self->ob_sval[i]); |
| 963 | } |
| 964 | else if (PySlice_Check(item)) { |
| 965 | Py_ssize_t start, stop, step, slicelength, cur, i; |
| 966 | char* source_buf; |
| 967 | char* result_buf; |
| 968 | PyObject* result; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 969 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 970 | if (PySlice_GetIndicesEx(item, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | PyBytes_GET_SIZE(self), |
| 972 | &start, &stop, &step, &slicelength) < 0) { |
| 973 | return NULL; |
| 974 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | if (slicelength <= 0) { |
| 977 | return PyBytes_FromStringAndSize("", 0); |
| 978 | } |
| 979 | else if (start == 0 && step == 1 && |
| 980 | slicelength == PyBytes_GET_SIZE(self) && |
| 981 | PyBytes_CheckExact(self)) { |
| 982 | Py_INCREF(self); |
| 983 | return (PyObject *)self; |
| 984 | } |
| 985 | else if (step == 1) { |
| 986 | return PyBytes_FromStringAndSize( |
| 987 | PyBytes_AS_STRING(self) + start, |
| 988 | slicelength); |
| 989 | } |
| 990 | else { |
| 991 | source_buf = PyBytes_AS_STRING(self); |
| 992 | result = PyBytes_FromStringAndSize(NULL, slicelength); |
| 993 | if (result == NULL) |
| 994 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | result_buf = PyBytes_AS_STRING(result); |
| 997 | for (cur = start, i = 0; i < slicelength; |
| 998 | cur += step, i++) { |
| 999 | result_buf[i] = source_buf[cur]; |
| 1000 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | return result; |
| 1003 | } |
| 1004 | } |
| 1005 | else { |
| 1006 | PyErr_Format(PyExc_TypeError, |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 1007 | "byte indices must be integers or slices, not %.200s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | Py_TYPE(item)->tp_name); |
| 1009 | return NULL; |
| 1010 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | static int |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1014 | bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1015 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self), |
| 1017 | 1, flags); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1020 | static PySequenceMethods bytes_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | (lenfunc)bytes_length, /*sq_length*/ |
| 1022 | (binaryfunc)bytes_concat, /*sq_concat*/ |
| 1023 | (ssizeargfunc)bytes_repeat, /*sq_repeat*/ |
| 1024 | (ssizeargfunc)bytes_item, /*sq_item*/ |
| 1025 | 0, /*sq_slice*/ |
| 1026 | 0, /*sq_ass_item*/ |
| 1027 | 0, /*sq_ass_slice*/ |
| 1028 | (objobjproc)bytes_contains /*sq_contains*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1029 | }; |
| 1030 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1031 | static PyMappingMethods bytes_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | (lenfunc)bytes_length, |
| 1033 | (binaryfunc)bytes_subscript, |
| 1034 | 0, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1035 | }; |
| 1036 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1037 | static PyBufferProcs bytes_as_buffer = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | (getbufferproc)bytes_buffer_getbuffer, |
| 1039 | NULL, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1040 | }; |
| 1041 | |
| 1042 | |
| 1043 | #define LEFTSTRIP 0 |
| 1044 | #define RIGHTSTRIP 1 |
| 1045 | #define BOTHSTRIP 2 |
| 1046 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1047 | /*[clinic input] |
| 1048 | bytes.split |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1049 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1050 | sep: object = None |
| 1051 | The delimiter according which to split the bytes. |
| 1052 | None (the default value) means split on ASCII whitespace characters |
| 1053 | (space, tab, return, newline, formfeed, vertical tab). |
| 1054 | maxsplit: Py_ssize_t = -1 |
| 1055 | Maximum number of splits to do. |
| 1056 | -1 (the default value) means no limit. |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1057 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1058 | Return a list of the sections in the bytes, using sep as the delimiter. |
| 1059 | [clinic start generated code]*/ |
| 1060 | |
| 1061 | PyDoc_STRVAR(bytes_split__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1062 | "split($self, /, sep=None, maxsplit=-1)\n" |
| 1063 | "--\n" |
| 1064 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1065 | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
| 1066 | "\n" |
| 1067 | " sep\n" |
| 1068 | " The delimiter according which to split the bytes.\n" |
| 1069 | " None (the default value) means split on ASCII whitespace characters\n" |
| 1070 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 1071 | " maxsplit\n" |
| 1072 | " Maximum number of splits to do.\n" |
| 1073 | " -1 (the default value) means no limit."); |
| 1074 | |
| 1075 | #define BYTES_SPLIT_METHODDEF \ |
| 1076 | {"split", (PyCFunction)bytes_split, METH_VARARGS|METH_KEYWORDS, bytes_split__doc__}, |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1077 | |
| 1078 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1079 | bytes_split_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1080 | |
| 1081 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1082 | bytes_split(PyBytesObject*self, PyObject *args, PyObject *kwargs) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1083 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1084 | PyObject *return_value = NULL; |
| 1085 | static char *_keywords[] = {"sep", "maxsplit", NULL}; |
| 1086 | PyObject *sep = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | Py_ssize_t maxsplit = -1; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1088 | |
| 1089 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 1090 | "|On:split", _keywords, |
| 1091 | &sep, &maxsplit)) |
| 1092 | goto exit; |
| 1093 | return_value = bytes_split_impl(self, sep, maxsplit); |
| 1094 | |
| 1095 | exit: |
| 1096 | return return_value; |
| 1097 | } |
| 1098 | |
| 1099 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1100 | bytes_split_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit) |
| 1101 | /*[clinic end generated code: output=c80a47afdd505975 input=8b809b39074abbfa]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1102 | { |
| 1103 | Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | const char *s = PyBytes_AS_STRING(self), *sub; |
| 1105 | Py_buffer vsub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1106 | PyObject *list; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | if (maxsplit < 0) |
| 1109 | maxsplit = PY_SSIZE_T_MAX; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1110 | if (sep == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1112 | if (_getbuffer(sep, &vsub) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | return NULL; |
| 1114 | sub = vsub.buf; |
| 1115 | n = vsub.len; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | list = stringlib_split((PyObject*) self, s, len, sub, n, maxsplit); |
| 1118 | PyBuffer_Release(&vsub); |
| 1119 | return list; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1122 | /*[clinic input] |
| 1123 | bytes.partition |
| 1124 | |
| 1125 | self: self(type="PyBytesObject *") |
| 1126 | sep: object |
| 1127 | / |
| 1128 | |
| 1129 | Partition the bytes into three parts using the given separator. |
| 1130 | |
| 1131 | This will search for the separator sep in the bytes. If the separator is found, |
| 1132 | returns a 3-tuple containing the part before the separator, the separator |
| 1133 | itself, and the part after it. |
| 1134 | |
| 1135 | If the separator is not found, returns a 3-tuple containing the original bytes |
| 1136 | object and two empty bytes objects. |
| 1137 | [clinic start generated code]*/ |
| 1138 | |
| 1139 | PyDoc_STRVAR(bytes_partition__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1140 | "partition($self, sep, /)\n" |
| 1141 | "--\n" |
| 1142 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1143 | "Partition the bytes into three parts using the given separator.\n" |
| 1144 | "\n" |
| 1145 | "This will search for the separator sep in the bytes. If the separator is found,\n" |
| 1146 | "returns a 3-tuple containing the part before the separator, the separator\n" |
| 1147 | "itself, and the part after it.\n" |
| 1148 | "\n" |
| 1149 | "If the separator is not found, returns a 3-tuple containing the original bytes\n" |
| 1150 | "object and two empty bytes objects."); |
| 1151 | |
| 1152 | #define BYTES_PARTITION_METHODDEF \ |
| 1153 | {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__}, |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1154 | |
| 1155 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1156 | bytes_partition(PyBytesObject *self, PyObject *sep) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1157 | /*[clinic end generated code: output=b41e119c873c08bc input=6c5b9dcc5a9fd62e]*/ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1158 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1159 | const char *sep_chars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | Py_ssize_t sep_len; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1161 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1162 | if (PyBytes_Check(sep)) { |
| 1163 | sep_chars = PyBytes_AS_STRING(sep); |
| 1164 | sep_len = PyBytes_GET_SIZE(sep); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1166 | else if (PyObject_AsCharBuffer(sep, &sep_chars, &sep_len)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | return stringlib_partition( |
| 1170 | (PyObject*) self, |
| 1171 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1172 | sep, sep_chars, sep_len |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | ); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1176 | /*[clinic input] |
| 1177 | bytes.rpartition |
| 1178 | |
| 1179 | self: self(type="PyBytesObject *") |
| 1180 | sep: object |
| 1181 | / |
| 1182 | |
| 1183 | Partition the bytes into three parts using the given separator. |
| 1184 | |
| 1185 | This will search for the separator sep in the bytes, starting and the end. If |
| 1186 | the separator is found, returns a 3-tuple containing the part before the |
| 1187 | separator, the separator itself, and the part after it. |
| 1188 | |
| 1189 | If the separator is not found, returns a 3-tuple containing two empty bytes |
| 1190 | objects and the original bytes object. |
| 1191 | [clinic start generated code]*/ |
| 1192 | |
| 1193 | PyDoc_STRVAR(bytes_rpartition__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1194 | "rpartition($self, sep, /)\n" |
| 1195 | "--\n" |
| 1196 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1197 | "Partition the bytes into three parts using the given separator.\n" |
| 1198 | "\n" |
| 1199 | "This will search for the separator sep in the bytes, starting and the end. If\n" |
| 1200 | "the separator is found, returns a 3-tuple containing the part before the\n" |
| 1201 | "separator, the separator itself, and the part after it.\n" |
| 1202 | "\n" |
| 1203 | "If the separator is not found, returns a 3-tuple containing two empty bytes\n" |
| 1204 | "objects and the original bytes object."); |
| 1205 | |
| 1206 | #define BYTES_RPARTITION_METHODDEF \ |
| 1207 | {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__}, |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1208 | |
| 1209 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1210 | bytes_rpartition(PyBytesObject *self, PyObject *sep) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1211 | /*[clinic end generated code: output=3a620803657196ee input=79bc2932e78e5ce0]*/ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1212 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1213 | const char *sep_chars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | Py_ssize_t sep_len; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1215 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1216 | if (PyBytes_Check(sep)) { |
| 1217 | sep_chars = PyBytes_AS_STRING(sep); |
| 1218 | sep_len = PyBytes_GET_SIZE(sep); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1220 | else if (PyObject_AsCharBuffer(sep, &sep_chars, &sep_len)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | return stringlib_rpartition( |
| 1224 | (PyObject*) self, |
| 1225 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1226 | sep, sep_chars, sep_len |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | ); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1230 | /*[clinic input] |
| 1231 | bytes.rsplit = bytes.split |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1232 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1233 | Return a list of the sections in the bytes, using sep as the delimiter. |
| 1234 | |
| 1235 | Splitting is done starting at the end of the bytes and working to the front. |
| 1236 | [clinic start generated code]*/ |
| 1237 | |
| 1238 | PyDoc_STRVAR(bytes_rsplit__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1239 | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
| 1240 | "--\n" |
| 1241 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1242 | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
| 1243 | "\n" |
| 1244 | " sep\n" |
| 1245 | " The delimiter according which to split the bytes.\n" |
| 1246 | " None (the default value) means split on ASCII whitespace characters\n" |
| 1247 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 1248 | " maxsplit\n" |
| 1249 | " Maximum number of splits to do.\n" |
| 1250 | " -1 (the default value) means no limit.\n" |
| 1251 | "\n" |
| 1252 | "Splitting is done starting at the end of the bytes and working to the front."); |
| 1253 | |
| 1254 | #define BYTES_RSPLIT_METHODDEF \ |
| 1255 | {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS|METH_KEYWORDS, bytes_rsplit__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1256 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1257 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1258 | bytes_rsplit_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1259 | |
| 1260 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1261 | bytes_rsplit(PyBytesObject*self, PyObject *args, PyObject *kwargs) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1262 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1263 | PyObject *return_value = NULL; |
| 1264 | static char *_keywords[] = {"sep", "maxsplit", NULL}; |
| 1265 | PyObject *sep = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | Py_ssize_t maxsplit = -1; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1267 | |
| 1268 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 1269 | "|On:rsplit", _keywords, |
| 1270 | &sep, &maxsplit)) |
| 1271 | goto exit; |
| 1272 | return_value = bytes_rsplit_impl(self, sep, maxsplit); |
| 1273 | |
| 1274 | exit: |
| 1275 | return return_value; |
| 1276 | } |
| 1277 | |
| 1278 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1279 | bytes_rsplit_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit) |
| 1280 | /*[clinic end generated code: output=f86feddedbd7b26d input=0f86c9f28f7d7b7b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1281 | { |
| 1282 | Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | const char *s = PyBytes_AS_STRING(self), *sub; |
| 1284 | Py_buffer vsub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1285 | PyObject *list; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | if (maxsplit < 0) |
| 1288 | maxsplit = PY_SSIZE_T_MAX; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1289 | if (sep == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1291 | if (_getbuffer(sep, &vsub) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | return NULL; |
| 1293 | sub = vsub.buf; |
| 1294 | n = vsub.len; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | list = stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit); |
| 1297 | PyBuffer_Release(&vsub); |
| 1298 | return list; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1301 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1302 | /*[clinic input] |
| 1303 | bytes.join |
| 1304 | |
| 1305 | iterable_of_bytes: object |
| 1306 | / |
| 1307 | |
| 1308 | Concatenate any number of bytes objects. |
| 1309 | |
| 1310 | The bytes whose method is called is inserted in between each pair. |
| 1311 | |
| 1312 | The result is returned as a new bytes object. |
| 1313 | |
| 1314 | Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'. |
| 1315 | [clinic start generated code]*/ |
| 1316 | |
| 1317 | PyDoc_STRVAR(bytes_join__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1318 | "join($self, iterable_of_bytes, /)\n" |
| 1319 | "--\n" |
| 1320 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1321 | "Concatenate any number of bytes objects.\n" |
| 1322 | "\n" |
| 1323 | "The bytes whose method is called is inserted in between each pair.\n" |
| 1324 | "\n" |
| 1325 | "The result is returned as a new bytes object.\n" |
| 1326 | "\n" |
| 1327 | "Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'."); |
| 1328 | |
| 1329 | #define BYTES_JOIN_METHODDEF \ |
| 1330 | {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1331 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1332 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1333 | bytes_join(PyBytesObject*self, PyObject *iterable_of_bytes) |
| 1334 | /*[clinic end generated code: output=e541a14a8da97908 input=7fe377b95bd549d2]*/ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1335 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1336 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1339 | PyObject * |
| 1340 | _PyBytes_Join(PyObject *sep, PyObject *x) |
| 1341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | assert(sep != NULL && PyBytes_Check(sep)); |
| 1343 | assert(x != NULL); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1344 | return bytes_join((PyBytesObject*)sep, x); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1347 | /* helper macro to fixup start/end slice values */ |
| 1348 | #define ADJUST_INDICES(start, end, len) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | if (end > len) \ |
| 1350 | end = len; \ |
| 1351 | else if (end < 0) { \ |
| 1352 | end += len; \ |
| 1353 | if (end < 0) \ |
| 1354 | end = 0; \ |
| 1355 | } \ |
| 1356 | if (start < 0) { \ |
| 1357 | start += len; \ |
| 1358 | if (start < 0) \ |
| 1359 | start = 0; \ |
| 1360 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1361 | |
| 1362 | Py_LOCAL_INLINE(Py_ssize_t) |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1363 | bytes_find_internal(PyBytesObject *self, PyObject *args, int dir) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1364 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | PyObject *subobj; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1366 | char byte; |
| 1367 | Py_buffer subbuf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | const char *sub; |
| 1369 | Py_ssize_t sub_len; |
| 1370 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1371 | Py_ssize_t res; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1372 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1373 | if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex", |
| 1374 | args, &subobj, &byte, &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | return -2; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1377 | if (subobj) { |
| 1378 | if (_getbuffer(subobj, &subbuf) < 0) |
| 1379 | return -2; |
| 1380 | |
| 1381 | sub = subbuf.buf; |
| 1382 | sub_len = subbuf.len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | } |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1384 | else { |
| 1385 | sub = &byte; |
| 1386 | sub_len = 1; |
| 1387 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | if (dir > 0) |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1390 | res = stringlib_find_slice( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
| 1392 | sub, sub_len, start, end); |
| 1393 | else |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1394 | res = stringlib_rfind_slice( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
| 1396 | sub, sub_len, start, end); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1397 | |
| 1398 | if (subobj) |
| 1399 | PyBuffer_Release(&subbuf); |
| 1400 | |
| 1401 | return res; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1406 | "B.find(sub[, start[, end]]) -> int\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1407 | \n\ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1408 | Return the lowest index in B where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1409 | such that sub is contained within B[start:end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1410 | arguments start and end are interpreted as in slice notation.\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1411 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1412 | Return -1 on failure."); |
| 1413 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1414 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1415 | bytes_find(PyBytesObject *self, PyObject *args) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1416 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | Py_ssize_t result = bytes_find_internal(self, args, +1); |
| 1418 | if (result == -2) |
| 1419 | return NULL; |
| 1420 | return PyLong_FromSsize_t(result); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1423 | |
| 1424 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1425 | "B.index(sub[, start[, end]]) -> int\n\ |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1426 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1427 | Like B.find() but raise ValueError when the substring is not found."); |
| 1428 | |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1429 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1430 | bytes_index(PyBytesObject *self, PyObject *args) |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | Py_ssize_t result = bytes_find_internal(self, args, +1); |
| 1433 | if (result == -2) |
| 1434 | return NULL; |
| 1435 | if (result == -1) { |
| 1436 | PyErr_SetString(PyExc_ValueError, |
| 1437 | "substring not found"); |
| 1438 | return NULL; |
| 1439 | } |
| 1440 | return PyLong_FromSsize_t(result); |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1443 | |
| 1444 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1445 | "B.rfind(sub[, start[, end]]) -> int\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1446 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1447 | Return the highest index in B where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1448 | such that sub is contained within B[start:end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1449 | arguments start and end are interpreted as in slice notation.\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1450 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1451 | Return -1 on failure."); |
| 1452 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1453 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1454 | bytes_rfind(PyBytesObject *self, PyObject *args) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | Py_ssize_t result = bytes_find_internal(self, args, -1); |
| 1457 | if (result == -2) |
| 1458 | return NULL; |
| 1459 | return PyLong_FromSsize_t(result); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1462 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1463 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1464 | "B.rindex(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1465 | \n\ |
| 1466 | Like B.rfind() but raise ValueError when the substring is not found."); |
| 1467 | |
| 1468 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1469 | bytes_rindex(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1470 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | Py_ssize_t result = bytes_find_internal(self, args, -1); |
| 1472 | if (result == -2) |
| 1473 | return NULL; |
| 1474 | if (result == -1) { |
| 1475 | PyErr_SetString(PyExc_ValueError, |
| 1476 | "substring not found"); |
| 1477 | return NULL; |
| 1478 | } |
| 1479 | return PyLong_FromSsize_t(result); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1482 | |
| 1483 | Py_LOCAL_INLINE(PyObject *) |
| 1484 | do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1485 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | Py_buffer vsep; |
| 1487 | char *s = PyBytes_AS_STRING(self); |
| 1488 | Py_ssize_t len = PyBytes_GET_SIZE(self); |
| 1489 | char *sep; |
| 1490 | Py_ssize_t seplen; |
| 1491 | Py_ssize_t i, j; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | if (_getbuffer(sepobj, &vsep) < 0) |
| 1494 | return NULL; |
| 1495 | sep = vsep.buf; |
| 1496 | seplen = vsep.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | i = 0; |
| 1499 | if (striptype != RIGHTSTRIP) { |
| 1500 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
| 1501 | i++; |
| 1502 | } |
| 1503 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | j = len; |
| 1506 | if (striptype != LEFTSTRIP) { |
| 1507 | do { |
| 1508 | j--; |
| 1509 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
| 1510 | j++; |
| 1511 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | PyBuffer_Release(&vsep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
| 1516 | Py_INCREF(self); |
| 1517 | return (PyObject*)self; |
| 1518 | } |
| 1519 | else |
| 1520 | return PyBytes_FromStringAndSize(s+i, j-i); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1523 | |
| 1524 | Py_LOCAL_INLINE(PyObject *) |
| 1525 | do_strip(PyBytesObject *self, int striptype) |
| 1526 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | char *s = PyBytes_AS_STRING(self); |
| 1528 | Py_ssize_t len = PyBytes_GET_SIZE(self), i, j; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1529 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | i = 0; |
| 1531 | if (striptype != RIGHTSTRIP) { |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1532 | while (i < len && Py_ISSPACE(s[i])) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | i++; |
| 1534 | } |
| 1535 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | j = len; |
| 1538 | if (striptype != LEFTSTRIP) { |
| 1539 | do { |
| 1540 | j--; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1541 | } while (j >= i && Py_ISSPACE(s[j])); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | j++; |
| 1543 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
| 1546 | Py_INCREF(self); |
| 1547 | return (PyObject*)self; |
| 1548 | } |
| 1549 | else |
| 1550 | return PyBytes_FromStringAndSize(s+i, j-i); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | |
| 1554 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1555 | do_argstrip(PyBytesObject *self, int striptype, PyObject *bytes) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1556 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1557 | if (bytes != NULL && bytes != Py_None) { |
| 1558 | return do_xstrip(self, striptype, bytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | } |
| 1560 | return do_strip(self, striptype); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1563 | /*[clinic input] |
| 1564 | bytes.strip |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1565 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1566 | self: self(type="PyBytesObject *") |
| 1567 | bytes: object = None |
| 1568 | / |
| 1569 | |
| 1570 | Strip leading and trailing bytes contained in the argument. |
| 1571 | |
| 1572 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 1573 | [clinic start generated code]*/ |
| 1574 | |
| 1575 | PyDoc_STRVAR(bytes_strip__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1576 | "strip($self, bytes=None, /)\n" |
| 1577 | "--\n" |
| 1578 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1579 | "Strip leading and trailing bytes contained in the argument.\n" |
| 1580 | "\n" |
| 1581 | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
| 1582 | |
| 1583 | #define BYTES_STRIP_METHODDEF \ |
| 1584 | {"strip", (PyCFunction)bytes_strip, METH_VARARGS, bytes_strip__doc__}, |
| 1585 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1586 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1587 | bytes_strip_impl(PyBytesObject *self, PyObject *bytes); |
| 1588 | |
| 1589 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1590 | bytes_strip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1591 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1592 | PyObject *return_value = NULL; |
| 1593 | PyObject *bytes = Py_None; |
| 1594 | |
| 1595 | if (!PyArg_UnpackTuple(args, "strip", |
| 1596 | 0, 1, |
| 1597 | &bytes)) |
| 1598 | goto exit; |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1599 | return_value = bytes_strip_impl(self, bytes); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1600 | |
| 1601 | exit: |
| 1602 | return return_value; |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1605 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1606 | bytes_strip_impl(PyBytesObject *self, PyObject *bytes) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1607 | /*[clinic end generated code: output=c8234a599ba5ec35 input=37daa5fad1395d95]*/ |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1608 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1609 | return do_argstrip(self, BOTHSTRIP, bytes); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1612 | /*[clinic input] |
| 1613 | bytes.lstrip |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1614 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1615 | self: self(type="PyBytesObject *") |
| 1616 | bytes: object = None |
| 1617 | / |
| 1618 | |
| 1619 | Strip leading bytes contained in the argument. |
| 1620 | |
| 1621 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 1622 | [clinic start generated code]*/ |
| 1623 | |
| 1624 | PyDoc_STRVAR(bytes_lstrip__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1625 | "lstrip($self, bytes=None, /)\n" |
| 1626 | "--\n" |
| 1627 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1628 | "Strip leading bytes contained in the argument.\n" |
| 1629 | "\n" |
| 1630 | "If the argument is omitted or None, strip leading ASCII whitespace."); |
| 1631 | |
| 1632 | #define BYTES_LSTRIP_METHODDEF \ |
| 1633 | {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, bytes_lstrip__doc__}, |
| 1634 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1635 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1636 | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 1637 | |
| 1638 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1639 | bytes_lstrip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1640 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1641 | PyObject *return_value = NULL; |
| 1642 | PyObject *bytes = Py_None; |
| 1643 | |
| 1644 | if (!PyArg_UnpackTuple(args, "lstrip", |
| 1645 | 0, 1, |
| 1646 | &bytes)) |
| 1647 | goto exit; |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1648 | return_value = bytes_lstrip_impl(self, bytes); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1649 | |
| 1650 | exit: |
| 1651 | return return_value; |
| 1652 | } |
| 1653 | |
| 1654 | static PyObject * |
| 1655 | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1656 | /*[clinic end generated code: output=529e8511ab6f1115 input=88811b09dfbc2988]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1657 | { |
| 1658 | return do_argstrip(self, LEFTSTRIP, bytes); |
| 1659 | } |
| 1660 | |
| 1661 | /*[clinic input] |
| 1662 | bytes.rstrip |
| 1663 | |
| 1664 | self: self(type="PyBytesObject *") |
| 1665 | bytes: object = None |
| 1666 | / |
| 1667 | |
| 1668 | Strip trailing bytes contained in the argument. |
| 1669 | |
| 1670 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 1671 | [clinic start generated code]*/ |
| 1672 | |
| 1673 | PyDoc_STRVAR(bytes_rstrip__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1674 | "rstrip($self, bytes=None, /)\n" |
| 1675 | "--\n" |
| 1676 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1677 | "Strip trailing bytes contained in the argument.\n" |
| 1678 | "\n" |
| 1679 | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
| 1680 | |
| 1681 | #define BYTES_RSTRIP_METHODDEF \ |
| 1682 | {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, bytes_rstrip__doc__}, |
| 1683 | |
| 1684 | static PyObject * |
| 1685 | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 1686 | |
| 1687 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1688 | bytes_rstrip(PyBytesObject *self, PyObject *args) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1689 | { |
| 1690 | PyObject *return_value = NULL; |
| 1691 | PyObject *bytes = Py_None; |
| 1692 | |
| 1693 | if (!PyArg_UnpackTuple(args, "rstrip", |
| 1694 | 0, 1, |
| 1695 | &bytes)) |
| 1696 | goto exit; |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1697 | return_value = bytes_rstrip_impl(self, bytes); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1698 | |
| 1699 | exit: |
| 1700 | return return_value; |
| 1701 | } |
| 1702 | |
| 1703 | static PyObject * |
| 1704 | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1705 | /*[clinic end generated code: output=e98730bd133e6593 input=8f93c9cd361f0140]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1706 | { |
| 1707 | return do_argstrip(self, RIGHTSTRIP, bytes); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1708 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1709 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1710 | |
| 1711 | PyDoc_STRVAR(count__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1712 | "B.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 1713 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1714 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1715 | string B[start:end]. Optional arguments start and end are interpreted\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1716 | as in slice notation."); |
| 1717 | |
| 1718 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1719 | bytes_count(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1720 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | PyObject *sub_obj; |
| 1722 | const char *str = PyBytes_AS_STRING(self), *sub; |
| 1723 | Py_ssize_t sub_len; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1724 | char byte; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1726 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1727 | Py_buffer vsub; |
| 1728 | PyObject *count_obj; |
| 1729 | |
| 1730 | if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte, |
| 1731 | &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1732 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1733 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1734 | if (sub_obj) { |
| 1735 | if (_getbuffer(sub_obj, &vsub) < 0) |
| 1736 | return NULL; |
| 1737 | |
| 1738 | sub = vsub.buf; |
| 1739 | sub_len = vsub.len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | } |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1741 | else { |
| 1742 | sub = &byte; |
| 1743 | sub_len = 1; |
| 1744 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1746 | ADJUST_INDICES(start, end, PyBytes_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1747 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1748 | count_obj = PyLong_FromSsize_t( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1749 | stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) |
| 1750 | ); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1751 | |
| 1752 | if (sub_obj) |
| 1753 | PyBuffer_Release(&vsub); |
| 1754 | |
| 1755 | return count_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1759 | /*[clinic input] |
| 1760 | bytes.translate |
| 1761 | |
| 1762 | self: self(type="PyBytesObject *") |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 1763 | table: object |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1764 | Translation table, which must be a bytes object of length 256. |
| 1765 | [ |
| 1766 | deletechars: object |
| 1767 | ] |
| 1768 | / |
| 1769 | |
| 1770 | Return a copy with each character mapped by the given translation table. |
| 1771 | |
| 1772 | All characters occurring in the optional argument deletechars are removed. |
| 1773 | The remaining characters are mapped through the given translation table. |
| 1774 | [clinic start generated code]*/ |
| 1775 | |
| 1776 | PyDoc_STRVAR(bytes_translate__doc__, |
| 1777 | "translate(table, [deletechars])\n" |
| 1778 | "Return a copy with each character mapped by the given translation table.\n" |
| 1779 | "\n" |
| 1780 | " table\n" |
| 1781 | " Translation table, which must be a bytes object of length 256.\n" |
| 1782 | "\n" |
| 1783 | "All characters occurring in the optional argument deletechars are removed.\n" |
| 1784 | "The remaining characters are mapped through the given translation table."); |
| 1785 | |
| 1786 | #define BYTES_TRANSLATE_METHODDEF \ |
| 1787 | {"translate", (PyCFunction)bytes_translate, METH_VARARGS, bytes_translate__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1788 | |
| 1789 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1790 | bytes_translate_impl(PyBytesObject *self, PyObject *table, int group_right_1, PyObject *deletechars); |
| 1791 | |
| 1792 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1793 | bytes_translate(PyBytesObject *self, PyObject *args) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1794 | { |
| 1795 | PyObject *return_value = NULL; |
| 1796 | PyObject *table; |
| 1797 | int group_right_1 = 0; |
| 1798 | PyObject *deletechars = NULL; |
| 1799 | |
| 1800 | switch (PyTuple_GET_SIZE(args)) { |
| 1801 | case 1: |
| 1802 | if (!PyArg_ParseTuple(args, "O:translate", &table)) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1803 | goto exit; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1804 | break; |
| 1805 | case 2: |
| 1806 | if (!PyArg_ParseTuple(args, "OO:translate", &table, &deletechars)) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1807 | goto exit; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1808 | group_right_1 = 1; |
| 1809 | break; |
| 1810 | default: |
| 1811 | PyErr_SetString(PyExc_TypeError, "bytes.translate requires 1 to 2 arguments"); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1812 | goto exit; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1813 | } |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1814 | return_value = bytes_translate_impl(self, table, group_right_1, deletechars); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1815 | |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1816 | exit: |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1817 | return return_value; |
| 1818 | } |
| 1819 | |
| 1820 | static PyObject * |
| 1821 | bytes_translate_impl(PyBytesObject *self, PyObject *table, int group_right_1, PyObject *deletechars) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1822 | /*[clinic end generated code: output=f0f29a57f41df5d8 input=a90fad893c3c88d7]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1823 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1824 | char *input, *output; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1825 | const char *table_chars; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1826 | Py_ssize_t i, c, changed = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | PyObject *input_obj = (PyObject*)self; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1828 | const char *output_start, *del_table_chars=NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | Py_ssize_t inlen, tablen, dellen = 0; |
| 1830 | PyObject *result; |
| 1831 | int trans_table[256]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1832 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1833 | if (PyBytes_Check(table)) { |
| 1834 | table_chars = PyBytes_AS_STRING(table); |
| 1835 | tablen = PyBytes_GET_SIZE(table); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1837 | else if (table == Py_None) { |
| 1838 | table_chars = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | tablen = 256; |
| 1840 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1841 | else if (PyObject_AsCharBuffer(table, &table_chars, &tablen)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | if (tablen != 256) { |
| 1845 | PyErr_SetString(PyExc_ValueError, |
| 1846 | "translation table must be 256 characters long"); |
| 1847 | return NULL; |
| 1848 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1849 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1850 | if (deletechars != NULL) { |
| 1851 | if (PyBytes_Check(deletechars)) { |
| 1852 | del_table_chars = PyBytes_AS_STRING(deletechars); |
| 1853 | dellen = PyBytes_GET_SIZE(deletechars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1855 | else if (PyObject_AsCharBuffer(deletechars, &del_table_chars, &dellen)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | return NULL; |
| 1857 | } |
| 1858 | else { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1859 | del_table_chars = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | dellen = 0; |
| 1861 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | inlen = PyBytes_GET_SIZE(input_obj); |
| 1864 | result = PyBytes_FromStringAndSize((char *)NULL, inlen); |
| 1865 | if (result == NULL) |
| 1866 | return NULL; |
| 1867 | output_start = output = PyBytes_AsString(result); |
| 1868 | input = PyBytes_AS_STRING(input_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1869 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1870 | if (dellen == 0 && table_chars != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | /* If no deletions are required, use faster code */ |
| 1872 | for (i = inlen; --i >= 0; ) { |
| 1873 | c = Py_CHARMASK(*input++); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1874 | if (Py_CHARMASK((*output++ = table_chars[c])) != c) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | changed = 1; |
| 1876 | } |
| 1877 | if (changed || !PyBytes_CheckExact(input_obj)) |
| 1878 | return result; |
| 1879 | Py_DECREF(result); |
| 1880 | Py_INCREF(input_obj); |
| 1881 | return input_obj; |
| 1882 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1883 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1884 | if (table_chars == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | for (i = 0; i < 256; i++) |
| 1886 | trans_table[i] = Py_CHARMASK(i); |
| 1887 | } else { |
| 1888 | for (i = 0; i < 256; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1889 | trans_table[i] = Py_CHARMASK(table_chars[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | for (i = 0; i < dellen; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1893 | trans_table[(int) Py_CHARMASK(del_table_chars[i])] = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | for (i = inlen; --i >= 0; ) { |
| 1896 | c = Py_CHARMASK(*input++); |
| 1897 | if (trans_table[c] != -1) |
| 1898 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1899 | continue; |
| 1900 | changed = 1; |
| 1901 | } |
| 1902 | if (!changed && PyBytes_CheckExact(input_obj)) { |
| 1903 | Py_DECREF(result); |
| 1904 | Py_INCREF(input_obj); |
| 1905 | return input_obj; |
| 1906 | } |
| 1907 | /* Fix the size of the resulting string */ |
| 1908 | if (inlen > 0) |
| 1909 | _PyBytes_Resize(&result, output - output_start); |
| 1910 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1914 | /*[clinic input] |
| 1915 | |
| 1916 | @staticmethod |
| 1917 | bytes.maketrans |
| 1918 | |
| 1919 | frm: object |
| 1920 | to: object |
| 1921 | / |
| 1922 | |
| 1923 | Return a translation table useable for the bytes or bytearray translate method. |
| 1924 | |
| 1925 | The returned table will be one where each byte in frm is mapped to the byte at |
| 1926 | the same position in to. |
| 1927 | |
| 1928 | The bytes objects frm and to must be of the same length. |
| 1929 | [clinic start generated code]*/ |
| 1930 | |
| 1931 | PyDoc_STRVAR(bytes_maketrans__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1932 | "maketrans(frm, to, /)\n" |
| 1933 | "--\n" |
| 1934 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1935 | "Return a translation table useable for the bytes or bytearray translate method.\n" |
| 1936 | "\n" |
| 1937 | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
| 1938 | "the same position in to.\n" |
| 1939 | "\n" |
| 1940 | "The bytes objects frm and to must be of the same length."); |
| 1941 | |
| 1942 | #define BYTES_MAKETRANS_METHODDEF \ |
| 1943 | {"maketrans", (PyCFunction)bytes_maketrans, METH_VARARGS|METH_STATIC, bytes_maketrans__doc__}, |
| 1944 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1945 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1946 | bytes_maketrans_impl(PyObject *frm, PyObject *to); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1947 | |
| 1948 | static PyObject * |
| 1949 | bytes_maketrans(void *null, PyObject *args) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1950 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1951 | PyObject *return_value = NULL; |
| 1952 | PyObject *frm; |
| 1953 | PyObject *to; |
| 1954 | |
| 1955 | if (!PyArg_UnpackTuple(args, "maketrans", |
| 1956 | 2, 2, |
| 1957 | &frm, &to)) |
| 1958 | goto exit; |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1959 | return_value = bytes_maketrans_impl(frm, to); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1960 | |
| 1961 | exit: |
| 1962 | return return_value; |
| 1963 | } |
| 1964 | |
| 1965 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1966 | bytes_maketrans_impl(PyObject *frm, PyObject *to) |
| 1967 | /*[clinic end generated code: output=89a3c3556975e466 input=d204f680f85da382]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1968 | { |
| 1969 | return _Py_bytes_maketrans(frm, to); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1972 | /* find and count characters and substrings */ |
| 1973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | #define findchar(target, target_len, c) \ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1975 | ((char *)memchr((const void *)(target), c, target_len)) |
| 1976 | |
| 1977 | /* String ops must return a string. */ |
| 1978 | /* If the object is subclass of string, create a copy */ |
| 1979 | Py_LOCAL(PyBytesObject *) |
| 1980 | return_self(PyBytesObject *self) |
| 1981 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1982 | if (PyBytes_CheckExact(self)) { |
| 1983 | Py_INCREF(self); |
| 1984 | return self; |
| 1985 | } |
| 1986 | return (PyBytesObject *)PyBytes_FromStringAndSize( |
| 1987 | PyBytes_AS_STRING(self), |
| 1988 | PyBytes_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | Py_LOCAL_INLINE(Py_ssize_t) |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1992 | countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1993 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | Py_ssize_t count=0; |
| 1995 | const char *start=target; |
| 1996 | const char *end=target+target_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 1999 | count++; |
| 2000 | if (count >= maxcount) |
| 2001 | break; |
| 2002 | start += 1; |
| 2003 | } |
| 2004 | return count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2007 | |
| 2008 | /* Algorithms for different cases of string replacement */ |
| 2009 | |
| 2010 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 2011 | Py_LOCAL(PyBytesObject *) |
| 2012 | replace_interleave(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2013 | const char *to_s, Py_ssize_t to_len, |
| 2014 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2015 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | char *self_s, *result_s; |
| 2017 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2018 | Py_ssize_t count, i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2021 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2022 | |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2023 | /* 1 at the end plus 1 after every character; |
| 2024 | count = min(maxcount, self_len + 1) */ |
| 2025 | if (maxcount <= self_len) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | count = maxcount; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2027 | else |
| 2028 | /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */ |
| 2029 | count = self_len + 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 | /* Check for overflow */ |
| 2032 | /* result_len = count * to_len + self_len; */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2033 | assert(count > 0); |
| 2034 | if (to_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | PyErr_SetString(PyExc_OverflowError, |
| 2036 | "replacement bytes are too long"); |
| 2037 | return NULL; |
| 2038 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2039 | result_len = count * to_len + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | if (! (result = (PyBytesObject *) |
| 2042 | PyBytes_FromStringAndSize(NULL, result_len)) ) |
| 2043 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | self_s = PyBytes_AS_STRING(self); |
| 2046 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | /* TODO: special case single character, which doesn't need memcpy */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | /* Lay the first one down (guaranteed this will occur) */ |
| 2051 | Py_MEMCPY(result_s, to_s, to_len); |
| 2052 | result_s += to_len; |
| 2053 | count -= 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | for (i=0; i<count; i++) { |
| 2056 | *result_s++ = *self_s++; |
| 2057 | Py_MEMCPY(result_s, to_s, to_len); |
| 2058 | result_s += to_len; |
| 2059 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | /* Copy the rest of the original string */ |
| 2062 | Py_MEMCPY(result_s, self_s, self_len-i); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /* Special case for deleting a single character */ |
| 2068 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 2069 | Py_LOCAL(PyBytesObject *) |
| 2070 | replace_delete_single_character(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2071 | char from_c, Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | char *self_s, *result_s; |
| 2074 | char *start, *next, *end; |
| 2075 | Py_ssize_t self_len, result_len; |
| 2076 | Py_ssize_t count; |
| 2077 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2079 | self_len = PyBytes_GET_SIZE(self); |
| 2080 | self_s = PyBytes_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | count = countchar(self_s, self_len, from_c, maxcount); |
| 2083 | if (count == 0) { |
| 2084 | return return_self(self); |
| 2085 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | result_len = self_len - count; /* from_len == 1 */ |
| 2088 | assert(result_len>=0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | if ( (result = (PyBytesObject *) |
| 2091 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
| 2092 | return NULL; |
| 2093 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | start = self_s; |
| 2096 | end = self_s + self_len; |
| 2097 | while (count-- > 0) { |
| 2098 | next = findchar(start, end-start, from_c); |
| 2099 | if (next == NULL) |
| 2100 | break; |
| 2101 | Py_MEMCPY(result_s, start, next-start); |
| 2102 | result_s += (next-start); |
| 2103 | start = next+1; |
| 2104 | } |
| 2105 | Py_MEMCPY(result_s, start, end-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 2111 | |
| 2112 | Py_LOCAL(PyBytesObject *) |
| 2113 | replace_delete_substring(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | const char *from_s, Py_ssize_t from_len, |
| 2115 | Py_ssize_t maxcount) { |
| 2116 | char *self_s, *result_s; |
| 2117 | char *start, *next, *end; |
| 2118 | Py_ssize_t self_len, result_len; |
| 2119 | Py_ssize_t count, offset; |
| 2120 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | self_len = PyBytes_GET_SIZE(self); |
| 2123 | self_s = PyBytes_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | count = stringlib_count(self_s, self_len, |
| 2126 | from_s, from_len, |
| 2127 | maxcount); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | if (count == 0) { |
| 2130 | /* no matches */ |
| 2131 | return return_self(self); |
| 2132 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | result_len = self_len - (count * from_len); |
| 2135 | assert (result_len>=0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | if ( (result = (PyBytesObject *) |
| 2138 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL ) |
| 2139 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2141 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | start = self_s; |
| 2144 | end = self_s + self_len; |
| 2145 | while (count-- > 0) { |
| 2146 | offset = stringlib_find(start, end-start, |
| 2147 | from_s, from_len, |
| 2148 | 0); |
| 2149 | if (offset == -1) |
| 2150 | break; |
| 2151 | next = start + offset; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | Py_MEMCPY(result_s, start, next-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | result_s += (next-start); |
| 2156 | start = next+from_len; |
| 2157 | } |
| 2158 | Py_MEMCPY(result_s, start, end-start); |
| 2159 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 2163 | Py_LOCAL(PyBytesObject *) |
| 2164 | replace_single_character_in_place(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | char from_c, char to_c, |
| 2166 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | char *self_s, *result_s, *start, *end, *next; |
| 2169 | Py_ssize_t self_len; |
| 2170 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2172 | /* The result string will be the same size */ |
| 2173 | self_s = PyBytes_AS_STRING(self); |
| 2174 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | next = findchar(self_s, self_len, from_c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | if (next == NULL) { |
| 2179 | /* No matches; return the original string */ |
| 2180 | return return_self(self); |
| 2181 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2183 | /* Need to make a new string */ |
| 2184 | result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
| 2185 | if (result == NULL) |
| 2186 | return NULL; |
| 2187 | result_s = PyBytes_AS_STRING(result); |
| 2188 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2190 | /* change everything in-place, starting with this one */ |
| 2191 | start = result_s + (next-self_s); |
| 2192 | *start = to_c; |
| 2193 | start++; |
| 2194 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | while (--maxcount > 0) { |
| 2197 | next = findchar(start, end-start, from_c); |
| 2198 | if (next == NULL) |
| 2199 | break; |
| 2200 | *next = to_c; |
| 2201 | start = next+1; |
| 2202 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 2208 | Py_LOCAL(PyBytesObject *) |
| 2209 | replace_substring_in_place(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2210 | const char *from_s, Py_ssize_t from_len, |
| 2211 | const char *to_s, Py_ssize_t to_len, |
| 2212 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | char *result_s, *start, *end; |
| 2215 | char *self_s; |
| 2216 | Py_ssize_t self_len, offset; |
| 2217 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | /* The result string will be the same size */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2221 | self_s = PyBytes_AS_STRING(self); |
| 2222 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | offset = stringlib_find(self_s, self_len, |
| 2225 | from_s, from_len, |
| 2226 | 0); |
| 2227 | if (offset == -1) { |
| 2228 | /* No matches; return the original string */ |
| 2229 | return return_self(self); |
| 2230 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | /* Need to make a new string */ |
| 2233 | result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
| 2234 | if (result == NULL) |
| 2235 | return NULL; |
| 2236 | result_s = PyBytes_AS_STRING(result); |
| 2237 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2239 | /* change everything in-place, starting with this one */ |
| 2240 | start = result_s + offset; |
| 2241 | Py_MEMCPY(start, to_s, from_len); |
| 2242 | start += from_len; |
| 2243 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2245 | while ( --maxcount > 0) { |
| 2246 | offset = stringlib_find(start, end-start, |
| 2247 | from_s, from_len, |
| 2248 | 0); |
| 2249 | if (offset==-1) |
| 2250 | break; |
| 2251 | Py_MEMCPY(start+offset, to_s, from_len); |
| 2252 | start += offset+from_len; |
| 2253 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 2259 | Py_LOCAL(PyBytesObject *) |
| 2260 | replace_single_character(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | char from_c, |
| 2262 | const char *to_s, Py_ssize_t to_len, |
| 2263 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | char *self_s, *result_s; |
| 2266 | char *start, *next, *end; |
| 2267 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2268 | Py_ssize_t count; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | self_s = PyBytes_AS_STRING(self); |
| 2272 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | count = countchar(self_s, self_len, from_c, maxcount); |
| 2275 | if (count == 0) { |
| 2276 | /* no matches, return unchanged */ |
| 2277 | return return_self(self); |
| 2278 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | /* use the difference between current and new, hence the "-1" */ |
| 2281 | /* result_len = self_len + count * (to_len-1) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2282 | assert(count > 0); |
| 2283 | if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | PyErr_SetString(PyExc_OverflowError, |
| 2285 | "replacement bytes are too long"); |
| 2286 | return NULL; |
| 2287 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2288 | result_len = self_len + count * (to_len - 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | if ( (result = (PyBytesObject *) |
| 2291 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
| 2292 | return NULL; |
| 2293 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | start = self_s; |
| 2296 | end = self_s + self_len; |
| 2297 | while (count-- > 0) { |
| 2298 | next = findchar(start, end-start, from_c); |
| 2299 | if (next == NULL) |
| 2300 | break; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | if (next == start) { |
| 2303 | /* replace with the 'to' */ |
| 2304 | Py_MEMCPY(result_s, to_s, to_len); |
| 2305 | result_s += to_len; |
| 2306 | start += 1; |
| 2307 | } else { |
| 2308 | /* copy the unchanged old then the 'to' */ |
| 2309 | Py_MEMCPY(result_s, start, next-start); |
| 2310 | result_s += (next-start); |
| 2311 | Py_MEMCPY(result_s, to_s, to_len); |
| 2312 | result_s += to_len; |
| 2313 | start = next+1; |
| 2314 | } |
| 2315 | } |
| 2316 | /* Copy the remainder of the remaining string */ |
| 2317 | Py_MEMCPY(result_s, start, end-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 2323 | Py_LOCAL(PyBytesObject *) |
| 2324 | replace_substring(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | const char *from_s, Py_ssize_t from_len, |
| 2326 | const char *to_s, Py_ssize_t to_len, |
| 2327 | Py_ssize_t maxcount) { |
| 2328 | char *self_s, *result_s; |
| 2329 | char *start, *next, *end; |
| 2330 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2331 | Py_ssize_t count, offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | self_s = PyBytes_AS_STRING(self); |
| 2335 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | count = stringlib_count(self_s, self_len, |
| 2338 | from_s, from_len, |
| 2339 | maxcount); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | if (count == 0) { |
| 2342 | /* no matches, return unchanged */ |
| 2343 | return return_self(self); |
| 2344 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | /* Check for overflow */ |
| 2347 | /* result_len = self_len + count * (to_len-from_len) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2348 | assert(count > 0); |
| 2349 | if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | PyErr_SetString(PyExc_OverflowError, |
| 2351 | "replacement bytes are too long"); |
| 2352 | return NULL; |
| 2353 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2354 | result_len = self_len + count * (to_len-from_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | if ( (result = (PyBytesObject *) |
| 2357 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
| 2358 | return NULL; |
| 2359 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | start = self_s; |
| 2362 | end = self_s + self_len; |
| 2363 | while (count-- > 0) { |
| 2364 | offset = stringlib_find(start, end-start, |
| 2365 | from_s, from_len, |
| 2366 | 0); |
| 2367 | if (offset == -1) |
| 2368 | break; |
| 2369 | next = start+offset; |
| 2370 | if (next == start) { |
| 2371 | /* replace with the 'to' */ |
| 2372 | Py_MEMCPY(result_s, to_s, to_len); |
| 2373 | result_s += to_len; |
| 2374 | start += from_len; |
| 2375 | } else { |
| 2376 | /* copy the unchanged old then the 'to' */ |
| 2377 | Py_MEMCPY(result_s, start, next-start); |
| 2378 | result_s += (next-start); |
| 2379 | Py_MEMCPY(result_s, to_s, to_len); |
| 2380 | result_s += to_len; |
| 2381 | start = next+from_len; |
| 2382 | } |
| 2383 | } |
| 2384 | /* Copy the remainder of the remaining string */ |
| 2385 | Py_MEMCPY(result_s, start, end-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | |
| 2391 | Py_LOCAL(PyBytesObject *) |
| 2392 | replace(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | const char *from_s, Py_ssize_t from_len, |
| 2394 | const char *to_s, Py_ssize_t to_len, |
| 2395 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2396 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | if (maxcount < 0) { |
| 2398 | maxcount = PY_SSIZE_T_MAX; |
| 2399 | } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) { |
| 2400 | /* nothing to do; return the original string */ |
| 2401 | return return_self(self); |
| 2402 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2404 | if (maxcount == 0 || |
| 2405 | (from_len == 0 && to_len == 0)) { |
| 2406 | /* nothing to do; return the original string */ |
| 2407 | return return_self(self); |
| 2408 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | /* Handle zero-length special cases */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2412 | if (from_len == 0) { |
| 2413 | /* insert the 'to' string everywhere. */ |
| 2414 | /* >>> "Python".replace("", ".") */ |
| 2415 | /* '.P.y.t.h.o.n.' */ |
| 2416 | return replace_interleave(self, to_s, to_len, maxcount); |
| 2417 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 2420 | /* point for an empty self string to generate a non-empty string */ |
| 2421 | /* Special case so the remaining code always gets a non-empty string */ |
| 2422 | if (PyBytes_GET_SIZE(self) == 0) { |
| 2423 | return return_self(self); |
| 2424 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2426 | if (to_len == 0) { |
| 2427 | /* delete all occurrences of 'from' string */ |
| 2428 | if (from_len == 1) { |
| 2429 | return replace_delete_single_character( |
| 2430 | self, from_s[0], maxcount); |
| 2431 | } else { |
| 2432 | return replace_delete_substring(self, from_s, |
| 2433 | from_len, maxcount); |
| 2434 | } |
| 2435 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | /* Handle special case where both strings have the same length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | if (from_len == to_len) { |
| 2440 | if (from_len == 1) { |
| 2441 | return replace_single_character_in_place( |
| 2442 | self, |
| 2443 | from_s[0], |
| 2444 | to_s[0], |
| 2445 | maxcount); |
| 2446 | } else { |
| 2447 | return replace_substring_in_place( |
| 2448 | self, from_s, from_len, to_s, to_len, |
| 2449 | maxcount); |
| 2450 | } |
| 2451 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | /* Otherwise use the more generic algorithms */ |
| 2454 | if (from_len == 1) { |
| 2455 | return replace_single_character(self, from_s[0], |
| 2456 | to_s, to_len, maxcount); |
| 2457 | } else { |
| 2458 | /* len('from')>=2, len('to')>=1 */ |
| 2459 | return replace_substring(self, from_s, from_len, to_s, to_len, |
| 2460 | maxcount); |
| 2461 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2464 | |
| 2465 | /*[clinic input] |
| 2466 | bytes.replace |
| 2467 | |
| 2468 | old: object |
| 2469 | new: object |
| 2470 | count: Py_ssize_t = -1 |
| 2471 | Maximum number of occurrences to replace. |
| 2472 | -1 (the default value) means replace all occurrences. |
| 2473 | / |
| 2474 | |
| 2475 | Return a copy with all occurrences of substring old replaced by new. |
| 2476 | |
| 2477 | If the optional argument count is given, only the first count occurrences are |
| 2478 | replaced. |
| 2479 | [clinic start generated code]*/ |
| 2480 | |
| 2481 | PyDoc_STRVAR(bytes_replace__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2482 | "replace($self, old, new, count=-1, /)\n" |
| 2483 | "--\n" |
| 2484 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2485 | "Return a copy with all occurrences of substring old replaced by new.\n" |
| 2486 | "\n" |
| 2487 | " count\n" |
| 2488 | " Maximum number of occurrences to replace.\n" |
| 2489 | " -1 (the default value) means replace all occurrences.\n" |
| 2490 | "\n" |
| 2491 | "If the optional argument count is given, only the first count occurrences are\n" |
| 2492 | "replaced."); |
| 2493 | |
| 2494 | #define BYTES_REPLACE_METHODDEF \ |
| 2495 | {"replace", (PyCFunction)bytes_replace, METH_VARARGS, bytes_replace__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2496 | |
| 2497 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2498 | bytes_replace_impl(PyBytesObject*self, PyObject *old, PyObject *new, Py_ssize_t count); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2499 | |
| 2500 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2501 | bytes_replace(PyBytesObject*self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2502 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2503 | PyObject *return_value = NULL; |
| 2504 | PyObject *old; |
| 2505 | PyObject *new; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2506 | Py_ssize_t count = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2507 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2508 | if (!PyArg_ParseTuple(args, |
| 2509 | "OO|n:replace", |
| 2510 | &old, &new, &count)) |
| 2511 | goto exit; |
| 2512 | return_value = bytes_replace_impl(self, old, new, count); |
| 2513 | |
| 2514 | exit: |
| 2515 | return return_value; |
| 2516 | } |
| 2517 | |
| 2518 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2519 | bytes_replace_impl(PyBytesObject*self, PyObject *old, PyObject *new, Py_ssize_t count) |
| 2520 | /*[clinic end generated code: output=14ce72f4f9cb91cf input=d3ac254ea50f4ac1]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2521 | { |
| 2522 | const char *old_s, *new_s; |
| 2523 | Py_ssize_t old_len, new_len; |
| 2524 | |
| 2525 | if (PyBytes_Check(old)) { |
| 2526 | old_s = PyBytes_AS_STRING(old); |
| 2527 | old_len = PyBytes_GET_SIZE(old); |
| 2528 | } |
| 2529 | else if (PyObject_AsCharBuffer(old, &old_s, &old_len)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2530 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2531 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2532 | if (PyBytes_Check(new)) { |
| 2533 | new_s = PyBytes_AS_STRING(new); |
| 2534 | new_len = PyBytes_GET_SIZE(new); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2535 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2536 | else if (PyObject_AsCharBuffer(new, &new_s, &new_len)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2537 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | return (PyObject *)replace((PyBytesObject *) self, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2540 | old_s, old_len, |
| 2541 | new_s, new_len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | /** End DALKE **/ |
| 2545 | |
| 2546 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
| 2547 | * against substr, using the start and end arguments. Returns |
| 2548 | * -1 on error, 0 if not found and 1 if found. |
| 2549 | */ |
| 2550 | Py_LOCAL(int) |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2551 | _bytes_tailmatch(PyBytesObject *self, PyObject *substr, Py_ssize_t start, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2552 | Py_ssize_t end, int direction) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2553 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2554 | Py_ssize_t len = PyBytes_GET_SIZE(self); |
| 2555 | Py_ssize_t slen; |
| 2556 | const char* sub; |
| 2557 | const char* str; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2559 | if (PyBytes_Check(substr)) { |
| 2560 | sub = PyBytes_AS_STRING(substr); |
| 2561 | slen = PyBytes_GET_SIZE(substr); |
| 2562 | } |
| 2563 | else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
| 2564 | return -1; |
| 2565 | str = PyBytes_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | ADJUST_INDICES(start, end, len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2569 | if (direction < 0) { |
| 2570 | /* startswith */ |
| 2571 | if (start+slen > len) |
| 2572 | return 0; |
| 2573 | } else { |
| 2574 | /* endswith */ |
| 2575 | if (end-start < slen || start > len) |
| 2576 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2578 | if (end-slen > start) |
| 2579 | start = end - slen; |
| 2580 | } |
| 2581 | if (end-start >= slen) |
| 2582 | return ! memcmp(str+start, sub, slen); |
| 2583 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | |
| 2587 | PyDoc_STRVAR(startswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2588 | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2589 | \n\ |
| 2590 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 2591 | With optional start, test B beginning at that position.\n\ |
| 2592 | With optional end, stop comparing B at that position.\n\ |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 2593 | prefix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2594 | |
| 2595 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2596 | bytes_startswith(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2598 | Py_ssize_t start = 0; |
| 2599 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 2600 | PyObject *subobj; |
| 2601 | int result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2602 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 2603 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | return NULL; |
| 2605 | if (PyTuple_Check(subobj)) { |
| 2606 | Py_ssize_t i; |
| 2607 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 2608 | result = _bytes_tailmatch(self, |
| 2609 | PyTuple_GET_ITEM(subobj, i), |
| 2610 | start, end, -1); |
| 2611 | if (result == -1) |
| 2612 | return NULL; |
| 2613 | else if (result) { |
| 2614 | Py_RETURN_TRUE; |
| 2615 | } |
| 2616 | } |
| 2617 | Py_RETURN_FALSE; |
| 2618 | } |
| 2619 | result = _bytes_tailmatch(self, subobj, start, end, -1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 2620 | if (result == -1) { |
| 2621 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2622 | PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes " |
| 2623 | "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2624 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 2625 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2626 | else |
| 2627 | return PyBool_FromLong(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | |
| 2631 | PyDoc_STRVAR(endswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2632 | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2633 | \n\ |
| 2634 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 2635 | With optional start, test B beginning at that position.\n\ |
| 2636 | With optional end, stop comparing B at that position.\n\ |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 2637 | suffix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2638 | |
| 2639 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2640 | bytes_endswith(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2641 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | Py_ssize_t start = 0; |
| 2643 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 2644 | PyObject *subobj; |
| 2645 | int result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2646 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 2647 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2648 | return NULL; |
| 2649 | if (PyTuple_Check(subobj)) { |
| 2650 | Py_ssize_t i; |
| 2651 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 2652 | result = _bytes_tailmatch(self, |
| 2653 | PyTuple_GET_ITEM(subobj, i), |
| 2654 | start, end, +1); |
| 2655 | if (result == -1) |
| 2656 | return NULL; |
| 2657 | else if (result) { |
| 2658 | Py_RETURN_TRUE; |
| 2659 | } |
| 2660 | } |
| 2661 | Py_RETURN_FALSE; |
| 2662 | } |
| 2663 | result = _bytes_tailmatch(self, subobj, start, end, +1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 2664 | if (result == -1) { |
| 2665 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2666 | PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " |
| 2667 | "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 2669 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | else |
| 2671 | return PyBool_FromLong(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2675 | /*[clinic input] |
| 2676 | bytes.decode |
| 2677 | |
| 2678 | encoding: str(c_default="NULL") = 'utf-8' |
| 2679 | The encoding with which to decode the bytes. |
| 2680 | errors: str(c_default="NULL") = 'strict' |
| 2681 | The error handling scheme to use for the handling of decoding errors. |
| 2682 | The default is 'strict' meaning that decoding errors raise a |
| 2683 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 2684 | as well as any other name registered with codecs.register_error that |
| 2685 | can handle UnicodeDecodeErrors. |
| 2686 | |
| 2687 | Decode the bytes using the codec registered for encoding. |
| 2688 | [clinic start generated code]*/ |
| 2689 | |
| 2690 | PyDoc_STRVAR(bytes_decode__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2691 | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
| 2692 | "--\n" |
| 2693 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2694 | "Decode the bytes using the codec registered for encoding.\n" |
| 2695 | "\n" |
| 2696 | " encoding\n" |
| 2697 | " The encoding with which to decode the bytes.\n" |
| 2698 | " errors\n" |
| 2699 | " The error handling scheme to use for the handling of decoding errors.\n" |
| 2700 | " The default is \'strict\' meaning that decoding errors raise a\n" |
| 2701 | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
| 2702 | " as well as any other name registered with codecs.register_error that\n" |
| 2703 | " can handle UnicodeDecodeErrors."); |
| 2704 | |
| 2705 | #define BYTES_DECODE_METHODDEF \ |
| 2706 | {"decode", (PyCFunction)bytes_decode, METH_VARARGS|METH_KEYWORDS, bytes_decode__doc__}, |
| 2707 | |
| 2708 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2709 | bytes_decode_impl(PyBytesObject*self, const char *encoding, const char *errors); |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2710 | |
| 2711 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2712 | bytes_decode(PyBytesObject*self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6f1fdc | 2007-04-12 22:49:52 +0000 | [diff] [blame] | 2713 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2714 | PyObject *return_value = NULL; |
| 2715 | static char *_keywords[] = {"encoding", "errors", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | const char *encoding = NULL; |
| 2717 | const char *errors = NULL; |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2718 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2719 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2720 | "|ss:decode", _keywords, |
| 2721 | &encoding, &errors)) |
| 2722 | goto exit; |
| 2723 | return_value = bytes_decode_impl(self, encoding, errors); |
| 2724 | |
| 2725 | exit: |
| 2726 | return return_value; |
| 2727 | } |
| 2728 | |
| 2729 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2730 | bytes_decode_impl(PyBytesObject*self, const char *encoding, const char *errors) |
| 2731 | /*[clinic end generated code: output=61a80290bbfce696 input=958174769d2a40ca]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2732 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2733 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2736 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2737 | /*[clinic input] |
| 2738 | bytes.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2739 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2740 | keepends: int(py_default="False") = 0 |
| 2741 | |
| 2742 | Return a list of the lines in the bytes, breaking at line boundaries. |
| 2743 | |
| 2744 | Line breaks are not included in the resulting list unless keepends is given and |
| 2745 | true. |
| 2746 | [clinic start generated code]*/ |
| 2747 | |
| 2748 | PyDoc_STRVAR(bytes_splitlines__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2749 | "splitlines($self, /, keepends=False)\n" |
| 2750 | "--\n" |
| 2751 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2752 | "Return a list of the lines in the bytes, breaking at line boundaries.\n" |
| 2753 | "\n" |
| 2754 | "Line breaks are not included in the resulting list unless keepends is given and\n" |
| 2755 | "true."); |
| 2756 | |
| 2757 | #define BYTES_SPLITLINES_METHODDEF \ |
| 2758 | {"splitlines", (PyCFunction)bytes_splitlines, METH_VARARGS|METH_KEYWORDS, bytes_splitlines__doc__}, |
| 2759 | |
| 2760 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2761 | bytes_splitlines_impl(PyBytesObject*self, int keepends); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2762 | |
| 2763 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2764 | bytes_splitlines(PyBytesObject*self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2765 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2766 | PyObject *return_value = NULL; |
| 2767 | static char *_keywords[] = {"keepends", NULL}; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2768 | int keepends = 0; |
| 2769 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2770 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2771 | "|i:splitlines", _keywords, |
| 2772 | &keepends)) |
| 2773 | goto exit; |
| 2774 | return_value = bytes_splitlines_impl(self, keepends); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2775 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2776 | exit: |
| 2777 | return return_value; |
| 2778 | } |
| 2779 | |
| 2780 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2781 | bytes_splitlines_impl(PyBytesObject*self, int keepends) |
| 2782 | /*[clinic end generated code: output=79da057d05d126de input=ddb93e3351080c8c]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2783 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2784 | return stringlib_splitlines( |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 2785 | (PyObject*) self, PyBytes_AS_STRING(self), |
| 2786 | PyBytes_GET_SIZE(self), keepends |
| 2787 | ); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2790 | static int |
Victor Stinner | 6430fd5 | 2011-09-29 04:02:13 +0200 | [diff] [blame] | 2791 | hex_digit_to_int(Py_UCS4 c) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | if (c >= 128) |
| 2794 | return -1; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2795 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | return c - '0'; |
| 2797 | else { |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2798 | if (Py_ISUPPER(c)) |
| 2799 | c = Py_TOLOWER(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | if (c >= 'a' && c <= 'f') |
| 2801 | return c - 'a' + 10; |
| 2802 | } |
| 2803 | return -1; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2806 | /*[clinic input] |
| 2807 | @classmethod |
| 2808 | bytes.fromhex |
| 2809 | |
| 2810 | string: unicode |
| 2811 | / |
| 2812 | |
| 2813 | Create a bytes object from a string of hexadecimal numbers. |
| 2814 | |
| 2815 | Spaces between two numbers are accepted. |
| 2816 | Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'. |
| 2817 | [clinic start generated code]*/ |
| 2818 | |
| 2819 | PyDoc_STRVAR(bytes_fromhex__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2820 | "fromhex($type, string, /)\n" |
| 2821 | "--\n" |
| 2822 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2823 | "Create a bytes object from a string of hexadecimal numbers.\n" |
| 2824 | "\n" |
| 2825 | "Spaces between two numbers are accepted.\n" |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2826 | "Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'."); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2827 | |
| 2828 | #define BYTES_FROMHEX_METHODDEF \ |
| 2829 | {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS, bytes_fromhex__doc__}, |
| 2830 | |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2831 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2832 | bytes_fromhex_impl(PyTypeObject *type, PyObject *string); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2833 | |
| 2834 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2835 | bytes_fromhex(PyTypeObject *type, PyObject *args) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2836 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2837 | PyObject *return_value = NULL; |
| 2838 | PyObject *string; |
| 2839 | |
| 2840 | if (!PyArg_ParseTuple(args, |
| 2841 | "U:fromhex", |
| 2842 | &string)) |
| 2843 | goto exit; |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2844 | return_value = bytes_fromhex_impl(type, string); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2845 | |
| 2846 | exit: |
| 2847 | return return_value; |
| 2848 | } |
| 2849 | |
| 2850 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2851 | bytes_fromhex_impl(PyTypeObject *type, PyObject *string) |
| 2852 | /*[clinic end generated code: output=09e6cbef56cbbb65 input=bf4d1c361670acd3]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2853 | { |
| 2854 | PyObject *newstring; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2856 | Py_ssize_t hexlen, byteslen, i, j; |
| 2857 | int top, bot; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2858 | void *data; |
| 2859 | unsigned int kind; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2860 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2861 | assert(PyUnicode_Check(string)); |
| 2862 | if (PyUnicode_READY(string)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2864 | kind = PyUnicode_KIND(string); |
| 2865 | data = PyUnicode_DATA(string); |
| 2866 | hexlen = PyUnicode_GET_LENGTH(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | byteslen = hexlen/2; /* This overestimates if there are spaces */ |
| 2869 | newstring = PyBytes_FromStringAndSize(NULL, byteslen); |
| 2870 | if (!newstring) |
| 2871 | return NULL; |
| 2872 | buf = PyBytes_AS_STRING(newstring); |
| 2873 | for (i = j = 0; i < hexlen; i += 2) { |
| 2874 | /* skip over spaces in the input */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2875 | while (PyUnicode_READ(kind, data, i) == ' ') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | i++; |
| 2877 | if (i >= hexlen) |
| 2878 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2879 | top = hex_digit_to_int(PyUnicode_READ(kind, data, i)); |
| 2880 | bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2881 | if (top == -1 || bot == -1) { |
| 2882 | PyErr_Format(PyExc_ValueError, |
| 2883 | "non-hexadecimal number found in " |
| 2884 | "fromhex() arg at position %zd", i); |
| 2885 | goto error; |
| 2886 | } |
| 2887 | buf[j++] = (top << 4) + bot; |
| 2888 | } |
| 2889 | if (j != byteslen && _PyBytes_Resize(&newstring, j) < 0) |
| 2890 | goto error; |
| 2891 | return newstring; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2892 | |
| 2893 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | Py_XDECREF(newstring); |
| 2895 | return NULL; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2898 | /*[clinic input] |
| 2899 | bytes.__sizeof__ as bytes_sizeof |
| 2900 | |
| 2901 | self: self(type="PyBytesObject *") |
| 2902 | |
| 2903 | Returns the size of the bytes object in memory, in bytes. |
| 2904 | [clinic start generated code]*/ |
| 2905 | |
| 2906 | PyDoc_STRVAR(bytes_sizeof__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2907 | "__sizeof__($self, /)\n" |
| 2908 | "--\n" |
| 2909 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2910 | "Returns the size of the bytes object in memory, in bytes."); |
| 2911 | |
| 2912 | #define BYTES_SIZEOF_METHODDEF \ |
| 2913 | {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, bytes_sizeof__doc__}, |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2914 | |
| 2915 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2916 | bytes_sizeof_impl(PyBytesObject *self); |
| 2917 | |
| 2918 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2919 | bytes_sizeof(PyBytesObject *self, PyObject *Py_UNUSED(ignored)) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2920 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2921 | return bytes_sizeof_impl(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | static PyObject * |
| 2925 | bytes_sizeof_impl(PyBytesObject *self) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2926 | /*[clinic end generated code: output=44933279343f24ae input=bee4c64bb42078ed]*/ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2927 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | Py_ssize_t res; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2929 | res = PyBytesObject_SIZE + Py_SIZE(self) * Py_TYPE(self)->tp_itemsize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2930 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 2933 | |
| 2934 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2935 | bytes_getnewargs(PyBytesObject *v) |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 2936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2937 | return Py_BuildValue("(y#)", v->ob_sval, Py_SIZE(v)); |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2940 | |
| 2941 | static PyMethodDef |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2942 | bytes_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS}, |
| 2944 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 2945 | _Py_capitalize__doc__}, |
| 2946 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
| 2947 | {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2948 | BYTES_DECODE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, |
| 2950 | endswith__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 2951 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | expandtabs__doc__}, |
| 2953 | {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2954 | BYTES_FROMHEX_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2955 | {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__}, |
| 2956 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 2957 | _Py_isalnum__doc__}, |
| 2958 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 2959 | _Py_isalpha__doc__}, |
| 2960 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 2961 | _Py_isdigit__doc__}, |
| 2962 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 2963 | _Py_islower__doc__}, |
| 2964 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 2965 | _Py_isspace__doc__}, |
| 2966 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 2967 | _Py_istitle__doc__}, |
| 2968 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 2969 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2970 | BYTES_JOIN_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2971 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 2972 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2973 | BYTES_LSTRIP_METHODDEF |
| 2974 | BYTES_MAKETRANS_METHODDEF |
| 2975 | BYTES_PARTITION_METHODDEF |
| 2976 | BYTES_REPLACE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__}, |
| 2978 | {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__}, |
| 2979 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2980 | BYTES_RPARTITION_METHODDEF |
| 2981 | BYTES_RSPLIT_METHODDEF |
| 2982 | BYTES_RSTRIP_METHODDEF |
| 2983 | BYTES_SPLIT_METHODDEF |
| 2984 | BYTES_SPLITLINES_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2985 | {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS, |
| 2986 | startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2987 | BYTES_STRIP_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 2989 | _Py_swapcase__doc__}, |
| 2990 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2991 | BYTES_TRANSLATE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2992 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
| 2993 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2994 | BYTES_SIZEOF_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2995 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2996 | }; |
| 2997 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2998 | static PyObject * |
| 2999 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3000 | |
| 3001 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3002 | bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3004 | PyObject *x = NULL; |
| 3005 | const char *encoding = NULL; |
| 3006 | const char *errors = NULL; |
| 3007 | PyObject *new = NULL; |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3008 | PyObject *func; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3009 | Py_ssize_t size; |
| 3010 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3011 | _Py_IDENTIFIER(__bytes__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | if (type != &PyBytes_Type) |
| 3014 | return str_subtype_new(type, args, kwds); |
| 3015 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x, |
| 3016 | &encoding, &errors)) |
| 3017 | return NULL; |
| 3018 | if (x == NULL) { |
| 3019 | if (encoding != NULL || errors != NULL) { |
| 3020 | PyErr_SetString(PyExc_TypeError, |
| 3021 | "encoding or errors without sequence " |
| 3022 | "argument"); |
| 3023 | return NULL; |
| 3024 | } |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3025 | return PyBytes_FromStringAndSize(NULL, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3026 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | if (PyUnicode_Check(x)) { |
| 3029 | /* Encode via the codec registry */ |
| 3030 | if (encoding == NULL) { |
| 3031 | PyErr_SetString(PyExc_TypeError, |
| 3032 | "string argument without an encoding"); |
| 3033 | return NULL; |
| 3034 | } |
| 3035 | new = PyUnicode_AsEncodedString(x, encoding, errors); |
| 3036 | if (new == NULL) |
| 3037 | return NULL; |
| 3038 | assert(PyBytes_Check(new)); |
| 3039 | return new; |
| 3040 | } |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3041 | |
| 3042 | /* We'd like to call PyObject_Bytes here, but we need to check for an |
| 3043 | integer argument before deferring to PyBytes_FromObject, something |
| 3044 | PyObject_Bytes doesn't do. */ |
| 3045 | func = _PyObject_LookupSpecial(x, &PyId___bytes__); |
| 3046 | if (func != NULL) { |
| 3047 | new = PyObject_CallFunctionObjArgs(func, NULL); |
| 3048 | Py_DECREF(func); |
| 3049 | if (new == NULL) |
| 3050 | return NULL; |
| 3051 | if (!PyBytes_Check(new)) { |
| 3052 | PyErr_Format(PyExc_TypeError, |
| 3053 | "__bytes__ returned non-bytes (type %.200s)", |
| 3054 | Py_TYPE(new)->tp_name); |
| 3055 | Py_DECREF(new); |
| 3056 | return NULL; |
| 3057 | } |
| 3058 | return new; |
| 3059 | } |
| 3060 | else if (PyErr_Occurred()) |
| 3061 | return NULL; |
| 3062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | /* Is it an integer? */ |
| 3064 | size = PyNumber_AsSsize_t(x, PyExc_OverflowError); |
| 3065 | if (size == -1 && PyErr_Occurred()) { |
| 3066 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 3067 | return NULL; |
| 3068 | PyErr_Clear(); |
| 3069 | } |
| 3070 | else if (size < 0) { |
| 3071 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 3072 | return NULL; |
| 3073 | } |
| 3074 | else { |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3075 | new = _PyBytes_FromSize(size, 1); |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3076 | if (new == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | return new; |
| 3079 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3080 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | /* If it's not unicode, there can't be encoding or errors */ |
| 3082 | if (encoding != NULL || errors != NULL) { |
| 3083 | PyErr_SetString(PyExc_TypeError, |
| 3084 | "encoding or errors without a string argument"); |
| 3085 | return NULL; |
| 3086 | } |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3087 | |
| 3088 | return PyBytes_FromObject(x); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | PyObject * |
| 3092 | PyBytes_FromObject(PyObject *x) |
| 3093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | PyObject *new, *it; |
| 3095 | Py_ssize_t i, size; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | if (x == NULL) { |
| 3098 | PyErr_BadInternalCall(); |
| 3099 | return NULL; |
| 3100 | } |
Larry Hastings | ca28e99 | 2012-05-24 22:58:30 -0700 | [diff] [blame] | 3101 | |
| 3102 | if (PyBytes_CheckExact(x)) { |
| 3103 | Py_INCREF(x); |
| 3104 | return x; |
| 3105 | } |
| 3106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3107 | /* Use the modern buffer interface */ |
| 3108 | if (PyObject_CheckBuffer(x)) { |
| 3109 | Py_buffer view; |
| 3110 | if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0) |
| 3111 | return NULL; |
| 3112 | new = PyBytes_FromStringAndSize(NULL, view.len); |
| 3113 | if (!new) |
| 3114 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval, |
| 3116 | &view, view.len, 'C') < 0) |
| 3117 | goto fail; |
| 3118 | PyBuffer_Release(&view); |
| 3119 | return new; |
| 3120 | fail: |
| 3121 | Py_XDECREF(new); |
| 3122 | PyBuffer_Release(&view); |
| 3123 | return NULL; |
| 3124 | } |
| 3125 | if (PyUnicode_Check(x)) { |
| 3126 | PyErr_SetString(PyExc_TypeError, |
| 3127 | "cannot convert unicode object to bytes"); |
| 3128 | return NULL; |
| 3129 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3131 | if (PyList_CheckExact(x)) { |
| 3132 | new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); |
| 3133 | if (new == NULL) |
| 3134 | return NULL; |
| 3135 | for (i = 0; i < Py_SIZE(x); i++) { |
| 3136 | Py_ssize_t value = PyNumber_AsSsize_t( |
| 3137 | PyList_GET_ITEM(x, i), PyExc_ValueError); |
| 3138 | if (value == -1 && PyErr_Occurred()) { |
| 3139 | Py_DECREF(new); |
| 3140 | return NULL; |
| 3141 | } |
| 3142 | if (value < 0 || value >= 256) { |
| 3143 | PyErr_SetString(PyExc_ValueError, |
| 3144 | "bytes must be in range(0, 256)"); |
| 3145 | Py_DECREF(new); |
| 3146 | return NULL; |
| 3147 | } |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 3148 | ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | } |
| 3150 | return new; |
| 3151 | } |
| 3152 | if (PyTuple_CheckExact(x)) { |
| 3153 | new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); |
| 3154 | if (new == NULL) |
| 3155 | return NULL; |
| 3156 | for (i = 0; i < Py_SIZE(x); i++) { |
| 3157 | Py_ssize_t value = PyNumber_AsSsize_t( |
| 3158 | PyTuple_GET_ITEM(x, i), PyExc_ValueError); |
| 3159 | if (value == -1 && PyErr_Occurred()) { |
| 3160 | Py_DECREF(new); |
| 3161 | return NULL; |
| 3162 | } |
| 3163 | if (value < 0 || value >= 256) { |
| 3164 | PyErr_SetString(PyExc_ValueError, |
| 3165 | "bytes must be in range(0, 256)"); |
| 3166 | Py_DECREF(new); |
| 3167 | return NULL; |
| 3168 | } |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 3169 | ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | } |
| 3171 | return new; |
| 3172 | } |
Alexandre Vassalotti | a5c565a | 2010-01-09 22:14:46 +0000 | [diff] [blame] | 3173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3174 | /* For iterator version, create a string object and resize as needed */ |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 3175 | size = PyObject_LengthHint(x, 64); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3176 | if (size == -1 && PyErr_Occurred()) |
| 3177 | return NULL; |
| 3178 | /* Allocate an extra byte to prevent PyBytes_FromStringAndSize() from |
| 3179 | returning a shared empty bytes string. This required because we |
| 3180 | want to call _PyBytes_Resize() the returned object, which we can |
| 3181 | only do on bytes objects with refcount == 1. */ |
Victor Stinner | 88d146b | 2014-08-17 21:12:18 +0200 | [diff] [blame] | 3182 | if (size == 0) |
| 3183 | size = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3184 | new = PyBytes_FromStringAndSize(NULL, size); |
| 3185 | if (new == NULL) |
| 3186 | return NULL; |
Victor Stinner | 88d146b | 2014-08-17 21:12:18 +0200 | [diff] [blame] | 3187 | assert(Py_REFCNT(new) == 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | /* Get the iterator */ |
| 3190 | it = PyObject_GetIter(x); |
| 3191 | if (it == NULL) |
| 3192 | goto error; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3194 | /* Run the iterator to exhaustion */ |
| 3195 | for (i = 0; ; i++) { |
| 3196 | PyObject *item; |
| 3197 | Py_ssize_t value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | /* Get the next item */ |
| 3200 | item = PyIter_Next(it); |
| 3201 | if (item == NULL) { |
| 3202 | if (PyErr_Occurred()) |
| 3203 | goto error; |
| 3204 | break; |
| 3205 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3207 | /* Interpret it as an int (__index__) */ |
| 3208 | value = PyNumber_AsSsize_t(item, PyExc_ValueError); |
| 3209 | Py_DECREF(item); |
| 3210 | if (value == -1 && PyErr_Occurred()) |
| 3211 | goto error; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3213 | /* Range check */ |
| 3214 | if (value < 0 || value >= 256) { |
| 3215 | PyErr_SetString(PyExc_ValueError, |
| 3216 | "bytes must be in range(0, 256)"); |
| 3217 | goto error; |
| 3218 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | /* Append the byte */ |
| 3221 | if (i >= size) { |
| 3222 | size = 2 * size + 1; |
| 3223 | if (_PyBytes_Resize(&new, size) < 0) |
| 3224 | goto error; |
| 3225 | } |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 3226 | ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | } |
| 3228 | _PyBytes_Resize(&new, i); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | /* Clean up and return success */ |
| 3231 | Py_DECREF(it); |
| 3232 | return new; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3233 | |
| 3234 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3235 | Py_XDECREF(it); |
Victor Stinner | 986e224 | 2013-10-29 03:14:22 +0100 | [diff] [blame] | 3236 | Py_XDECREF(new); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | static PyObject * |
| 3241 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3243 | PyObject *tmp, *pnew; |
| 3244 | Py_ssize_t n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3246 | assert(PyType_IsSubtype(type, &PyBytes_Type)); |
| 3247 | tmp = bytes_new(&PyBytes_Type, args, kwds); |
| 3248 | if (tmp == NULL) |
| 3249 | return NULL; |
| 3250 | assert(PyBytes_CheckExact(tmp)); |
| 3251 | n = PyBytes_GET_SIZE(tmp); |
| 3252 | pnew = type->tp_alloc(type, n); |
| 3253 | if (pnew != NULL) { |
| 3254 | Py_MEMCPY(PyBytes_AS_STRING(pnew), |
| 3255 | PyBytes_AS_STRING(tmp), n+1); |
| 3256 | ((PyBytesObject *)pnew)->ob_shash = |
| 3257 | ((PyBytesObject *)tmp)->ob_shash; |
| 3258 | } |
| 3259 | Py_DECREF(tmp); |
| 3260 | return pnew; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3263 | PyDoc_STRVAR(bytes_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3264 | "bytes(iterable_of_ints) -> bytes\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3265 | bytes(string, encoding[, errors]) -> bytes\n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3266 | bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3267 | bytes(int) -> bytes object of size given by the parameter initialized with null bytes\n\ |
| 3268 | bytes() -> empty bytes object\n\ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3269 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3270 | Construct an immutable array of bytes from:\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3271 | - an iterable yielding integers in range(256)\n\ |
| 3272 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3273 | - any object implementing the buffer API.\n\ |
| 3274 | - an integer"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3275 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3276 | static PyObject *bytes_iter(PyObject *seq); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3277 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3278 | PyTypeObject PyBytes_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3279 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3280 | "bytes", |
| 3281 | PyBytesObject_SIZE, |
| 3282 | sizeof(char), |
| 3283 | bytes_dealloc, /* tp_dealloc */ |
| 3284 | 0, /* tp_print */ |
| 3285 | 0, /* tp_getattr */ |
| 3286 | 0, /* tp_setattr */ |
| 3287 | 0, /* tp_reserved */ |
| 3288 | (reprfunc)bytes_repr, /* tp_repr */ |
| 3289 | 0, /* tp_as_number */ |
| 3290 | &bytes_as_sequence, /* tp_as_sequence */ |
| 3291 | &bytes_as_mapping, /* tp_as_mapping */ |
| 3292 | (hashfunc)bytes_hash, /* tp_hash */ |
| 3293 | 0, /* tp_call */ |
| 3294 | bytes_str, /* tp_str */ |
| 3295 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3296 | 0, /* tp_setattro */ |
| 3297 | &bytes_as_buffer, /* tp_as_buffer */ |
| 3298 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 3299 | Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */ |
| 3300 | bytes_doc, /* tp_doc */ |
| 3301 | 0, /* tp_traverse */ |
| 3302 | 0, /* tp_clear */ |
| 3303 | (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
| 3304 | 0, /* tp_weaklistoffset */ |
| 3305 | bytes_iter, /* tp_iter */ |
| 3306 | 0, /* tp_iternext */ |
| 3307 | bytes_methods, /* tp_methods */ |
| 3308 | 0, /* tp_members */ |
| 3309 | 0, /* tp_getset */ |
| 3310 | &PyBaseObject_Type, /* tp_base */ |
| 3311 | 0, /* tp_dict */ |
| 3312 | 0, /* tp_descr_get */ |
| 3313 | 0, /* tp_descr_set */ |
| 3314 | 0, /* tp_dictoffset */ |
| 3315 | 0, /* tp_init */ |
| 3316 | 0, /* tp_alloc */ |
| 3317 | bytes_new, /* tp_new */ |
| 3318 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3319 | }; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3320 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3321 | void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3322 | PyBytes_Concat(PyObject **pv, PyObject *w) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3323 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | assert(pv != NULL); |
| 3325 | if (*pv == NULL) |
| 3326 | return; |
| 3327 | if (w == NULL) { |
Serhiy Storchaka | f458a03 | 2013-02-02 18:45:22 +0200 | [diff] [blame] | 3328 | Py_CLEAR(*pv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | return; |
| 3330 | } |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 3331 | |
| 3332 | if (Py_REFCNT(*pv) == 1 && PyBytes_CheckExact(*pv)) { |
| 3333 | /* Only one reference, so we can resize in place */ |
Zachary Ware | bca9694 | 2014-05-06 11:42:37 -0500 | [diff] [blame] | 3334 | Py_ssize_t oldsize; |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 3335 | Py_buffer wb; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 3336 | |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 3337 | wb.len = -1; |
| 3338 | if (_getbuffer(w, &wb) < 0) { |
| 3339 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 3340 | Py_TYPE(w)->tp_name, Py_TYPE(*pv)->tp_name); |
| 3341 | Py_CLEAR(*pv); |
| 3342 | return; |
| 3343 | } |
| 3344 | |
| 3345 | oldsize = PyBytes_GET_SIZE(*pv); |
| 3346 | if (oldsize > PY_SSIZE_T_MAX - wb.len) { |
| 3347 | PyErr_NoMemory(); |
| 3348 | goto error; |
| 3349 | } |
| 3350 | if (_PyBytes_Resize(pv, oldsize + wb.len) < 0) |
| 3351 | goto error; |
| 3352 | |
| 3353 | memcpy(PyBytes_AS_STRING(*pv) + oldsize, wb.buf, wb.len); |
| 3354 | PyBuffer_Release(&wb); |
| 3355 | return; |
| 3356 | |
| 3357 | error: |
| 3358 | PyBuffer_Release(&wb); |
| 3359 | Py_CLEAR(*pv); |
| 3360 | return; |
| 3361 | } |
| 3362 | |
| 3363 | else { |
| 3364 | /* Multiple references, need to create new object */ |
| 3365 | PyObject *v; |
| 3366 | v = bytes_concat(*pv, w); |
| 3367 | Py_DECREF(*pv); |
| 3368 | *pv = v; |
| 3369 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3373 | PyBytes_ConcatAndDel(PyObject **pv, PyObject *w) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3375 | PyBytes_Concat(pv, w); |
| 3376 | Py_XDECREF(w); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | |
| 3380 | /* The following function breaks the notion that strings are immutable: |
| 3381 | it changes the size of a string. We get away with this only if there |
| 3382 | is only one module referencing the object. You can also think of it |
| 3383 | as creating a new string object and destroying the old one, only |
| 3384 | more efficiently. In any case, don't use this if the string may |
| 3385 | already be known to some other part of the code... |
| 3386 | Note that if there's not enough memory to resize the string, the original |
| 3387 | string object at *pv is deallocated, *pv is set to NULL, an "out of |
| 3388 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 3389 | returned, and the value in *pv may or may not be the same as on input. |
| 3390 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 3391 | does *not* include that), and a trailing \0 byte is stored. |
| 3392 | */ |
| 3393 | |
| 3394 | int |
| 3395 | _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize) |
| 3396 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3397 | PyObject *v; |
| 3398 | PyBytesObject *sv; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3399 | v = *pv; |
| 3400 | if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) { |
| 3401 | *pv = 0; |
| 3402 | Py_DECREF(v); |
| 3403 | PyErr_BadInternalCall(); |
| 3404 | return -1; |
| 3405 | } |
| 3406 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 3407 | _Py_DEC_REFTOTAL; |
| 3408 | _Py_ForgetReference(v); |
| 3409 | *pv = (PyObject *) |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 3410 | PyObject_REALLOC(v, PyBytesObject_SIZE + newsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3411 | if (*pv == NULL) { |
| 3412 | PyObject_Del(v); |
| 3413 | PyErr_NoMemory(); |
| 3414 | return -1; |
| 3415 | } |
| 3416 | _Py_NewReference(*pv); |
| 3417 | sv = (PyBytesObject *) *pv; |
| 3418 | Py_SIZE(sv) = newsize; |
| 3419 | sv->ob_sval[newsize] = '\0'; |
| 3420 | sv->ob_shash = -1; /* invalidate cached hash value */ |
| 3421 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3422 | } |
| 3423 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3424 | void |
| 3425 | PyBytes_Fini(void) |
| 3426 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3427 | int i; |
Serhiy Storchaka | f458a03 | 2013-02-02 18:45:22 +0200 | [diff] [blame] | 3428 | for (i = 0; i < UCHAR_MAX + 1; i++) |
| 3429 | Py_CLEAR(characters[i]); |
| 3430 | Py_CLEAR(nullstring); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 3433 | /*********************** Bytes Iterator ****************************/ |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3434 | |
| 3435 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | PyObject_HEAD |
| 3437 | Py_ssize_t it_index; |
| 3438 | PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3439 | } striterobject; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3440 | |
| 3441 | static void |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3442 | striter_dealloc(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | _PyObject_GC_UNTRACK(it); |
| 3445 | Py_XDECREF(it->it_seq); |
| 3446 | PyObject_GC_Del(it); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | static int |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3450 | striter_traverse(striterobject *it, visitproc visit, void *arg) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | Py_VISIT(it->it_seq); |
| 3453 | return 0; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | static PyObject * |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3457 | striter_next(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | PyBytesObject *seq; |
| 3460 | PyObject *item; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3462 | assert(it != NULL); |
| 3463 | seq = it->it_seq; |
| 3464 | if (seq == NULL) |
| 3465 | return NULL; |
| 3466 | assert(PyBytes_Check(seq)); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | if (it->it_index < PyBytes_GET_SIZE(seq)) { |
| 3469 | item = PyLong_FromLong( |
| 3470 | (unsigned char)seq->ob_sval[it->it_index]); |
| 3471 | if (item != NULL) |
| 3472 | ++it->it_index; |
| 3473 | return item; |
| 3474 | } |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3476 | Py_DECREF(seq); |
| 3477 | it->it_seq = NULL; |
| 3478 | return NULL; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | static PyObject * |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3482 | striter_len(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3483 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3484 | Py_ssize_t len = 0; |
| 3485 | if (it->it_seq) |
| 3486 | len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
| 3487 | return PyLong_FromSsize_t(len); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | PyDoc_STRVAR(length_hint_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | "Private method returning an estimate of len(list(it))."); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3492 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3493 | static PyObject * |
| 3494 | striter_reduce(striterobject *it) |
| 3495 | { |
| 3496 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3497 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3498 | it->it_seq, it->it_index); |
| 3499 | } else { |
| 3500 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 3501 | if (u == NULL) |
| 3502 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3503 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 3508 | |
| 3509 | static PyObject * |
| 3510 | striter_setstate(striterobject *it, PyObject *state) |
| 3511 | { |
| 3512 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 3513 | if (index == -1 && PyErr_Occurred()) |
| 3514 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 3515 | if (it->it_seq != NULL) { |
| 3516 | if (index < 0) |
| 3517 | index = 0; |
| 3518 | else if (index > PyBytes_GET_SIZE(it->it_seq)) |
| 3519 | index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 3520 | it->it_index = index; |
| 3521 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3522 | Py_RETURN_NONE; |
| 3523 | } |
| 3524 | |
| 3525 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 3526 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3527 | static PyMethodDef striter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3528 | {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS, |
| 3529 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3530 | {"__reduce__", (PyCFunction)striter_reduce, METH_NOARGS, |
| 3531 | reduce_doc}, |
| 3532 | {"__setstate__", (PyCFunction)striter_setstate, METH_O, |
| 3533 | setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3534 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3535 | }; |
| 3536 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3537 | PyTypeObject PyBytesIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3538 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3539 | "bytes_iterator", /* tp_name */ |
| 3540 | sizeof(striterobject), /* tp_basicsize */ |
| 3541 | 0, /* tp_itemsize */ |
| 3542 | /* methods */ |
| 3543 | (destructor)striter_dealloc, /* tp_dealloc */ |
| 3544 | 0, /* tp_print */ |
| 3545 | 0, /* tp_getattr */ |
| 3546 | 0, /* tp_setattr */ |
| 3547 | 0, /* tp_reserved */ |
| 3548 | 0, /* tp_repr */ |
| 3549 | 0, /* tp_as_number */ |
| 3550 | 0, /* tp_as_sequence */ |
| 3551 | 0, /* tp_as_mapping */ |
| 3552 | 0, /* tp_hash */ |
| 3553 | 0, /* tp_call */ |
| 3554 | 0, /* tp_str */ |
| 3555 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3556 | 0, /* tp_setattro */ |
| 3557 | 0, /* tp_as_buffer */ |
| 3558 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3559 | 0, /* tp_doc */ |
| 3560 | (traverseproc)striter_traverse, /* tp_traverse */ |
| 3561 | 0, /* tp_clear */ |
| 3562 | 0, /* tp_richcompare */ |
| 3563 | 0, /* tp_weaklistoffset */ |
| 3564 | PyObject_SelfIter, /* tp_iter */ |
| 3565 | (iternextfunc)striter_next, /* tp_iternext */ |
| 3566 | striter_methods, /* tp_methods */ |
| 3567 | 0, |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3568 | }; |
| 3569 | |
| 3570 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3571 | bytes_iter(PyObject *seq) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3573 | striterobject *it; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3575 | if (!PyBytes_Check(seq)) { |
| 3576 | PyErr_BadInternalCall(); |
| 3577 | return NULL; |
| 3578 | } |
| 3579 | it = PyObject_GC_New(striterobject, &PyBytesIter_Type); |
| 3580 | if (it == NULL) |
| 3581 | return NULL; |
| 3582 | it->it_index = 0; |
| 3583 | Py_INCREF(seq); |
| 3584 | it->it_seq = (PyBytesObject *)seq; |
| 3585 | _PyObject_GC_TRACK(it); |
| 3586 | return (PyObject *)it; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3587 | } |