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