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