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